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)
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eab66b-4f522.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eab66b-4f522.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?