Queue execution of promise function

//Promise
function app(num,data){
 
}

if you want to execute yourself again after a successful call

var data=[...]
//data.length=3
app(0)
.then(function(res){
    console.log(res)
    return app(1)
})
.then(function(res){
     console.log(res)
    return app(2)
});

if the number of times of execution is not sure how to implement it, it has been executed for 3 times, and the length of the returned data data is uncertain.

Mar.09,2021

if you are not connected to each other every time you execute app (), you can put it in an array and use Promise.all to execute

.
function app(num, data) {
    return new Promise(function (resolve, reject) {
        resolve(num)
    })
}

function test(arr, cb) {
    return arr.reduce((p, v) => p.then(() => cb(v)), Promise.resolve())
}

test([4, 1, 9], function (num) {
    return app(num).then(res => {
        console.log(res)
    });
})
Menu