Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  183] [ 2]  / answers: 1 / hits: 23364  / 7 Years ago, mon, february 20, 2017, 12:00:00

I have several promises that I need to resolve before going further.



Promise.all(promises).then((results) => {
// going further
});


Is there any way I can have the progress of the Promise.all promise?



From the doc, it appears that it is not possible. And this question doesn't answer it either.



So:




  • Don't you agree that this would be useful? Shouldn't we query for this feature?

  • How can one implement it manually for now?


More From » es6-promise

 Answers
36

I've knocked up a little helper function that you can re-use.



Basically pass your promises as normal, and provide a callback to do what you want with the progress..





function allProgress(proms, progress_cb) {
let d = 0;
progress_cb(0);
for (const p of proms) {
p.then(()=> {
d ++;
progress_cb( (d * 100) / proms.length );
});
}
return Promise.all(proms);
}

function test(ms) {
return new Promise((resolve) => {
setTimeout(() => {
console.log(`Waited ${ms}`);
resolve();
}, ms);
});
}


allProgress([test(1000), test(3000), test(2000), test(3500)],
(p) => {
console.log(`% Done = ${p.toFixed(2)}`);
});




[#58862] Friday, February 17, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
koltonadolfow

Total Points: 71
Total Questions: 118
Total Answers: 102

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;