JavaScript Promise.all - how to check resolve status?
No need for using the setInterval
. Only update the progress when it is updated.
const promises = views.map(view => fetch (`/sites/${siteId}/views/${view.id}/image/`));
const images = Promise.all(promises);
let progress = 0;
promises.forEach(p => p.then(() => {
progress++;
console.log(progress / promises.length * 100 + "%");
}));
Just increment a variable whenever a promise resolves:
const promises = views.map(view => fetch (`/sites/${siteId}/views/${view.id}/image/`));
const images = Promise.all(promises);
let progress = 0;
promises.forEach(p => p.then(() => progress++));
setInterval(() => {
console.log(progress / promises.length * 100 + "%");
}, 1000);