最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - JavaScript Promise.all - how to check resolve status? - Stack Overflow

programmeradmin0浏览0评论

Let's say, I have an array of promises, each element beign an AJAX call for an image (png) of a view.

const images = Promise.all(views.map(view => {
   return fetch(`/sites/${siteId}/views/${view.id}/image/`);
}));

Is there any possibility to check current status of promise resolution using Promise.all? If not, is there any other way?

For example, if 10 / 20 images were downloaded, I would like to give user a feedback, that we have downloaded 50% images for him.

Let's say, I have an array of promises, each element beign an AJAX call for an image (png) of a view.

const images = Promise.all(views.map(view => {
   return fetch(`/sites/${siteId}/views/${view.id}/image/`);
}));

Is there any possibility to check current status of promise resolution using Promise.all? If not, is there any other way?

For example, if 10 / 20 images were downloaded, I would like to give user a feedback, that we have downloaded 50% images for him.

Share Improve this question asked Jun 1, 2018 at 15:09 Rafał BagrowskiRafał Bagrowski 2434 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

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

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 + "%");
}));
发布评论

评论列表(0)

  1. 暂无评论