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);