Promise.all loop execution problem

setTimeout internal demand needs to put a request. How to make sure that the next time it is executed is to make sure that the result is returned before

 var array=[];
    function output(j) {
        return new Promise( function(resolve, reject) {
            setTimeout(function () {
                console.log("",j)
                return resolve(j);
            },1000 * j);
        }).then(function (res) {
            return Promise.resolve(res)
        });
    }

    for (var i=0;i<5;iPP){
        array.push(output(i));
    }
    Promise.all(array).then(function (res) {
        console.log("res",res);
May.22,2021

do you mean that the previous promise is finished before the next one is called?

for (var i=0;i<5;iPP){
    array.push(output(i));
}

you have all called

here.

Sequential execution can be performed using reduce

 var array=[];
 var result = [];
function output(j) {
    j = j+1//
    return new Promise( function(resolve, reject) {
        setTimeout(function () {
            console.log('',j)
            return resolve(j);
        },1000 * j);
    }).then(function (res) {
        result.push(res);// push
        return Promise.resolve(res)
    });
}
for (var i=0;i<5;iPP){
    array.push(output);//push 
}

var lastPromise = array.reduce((p,v)=>p.then(v),Promise.resolve(0))// 
lastPromise.then(v=>{
  console.log(result)
})
Menu