简体   繁体   English

在 Promise.all() 中附加 .then() 的 Promise

[英]Promise attached with .then() in Promise.all()

I am confused about the resolution of promises.我对承诺的解决感到困惑。 If I created an array of promises and supplied a .then() to all of them as shown below..如果我创建了一个 Promise 数组并为它们提供了一个 .then() ,如下所示..

// assume async() returns a promise that resolves with the number 5. 
promiseArray = [];
for (let i = 0; i < 5; i++){
promiseArray.push(async(5).then(() => console.log("done")));
}
Promise.all(promiseArray).then(() => console.log("all done"));

I am confused about what is happening here.我对这里发生的事情感到困惑。 Do the promises start console logging "done" even before Promise.all() completes?承诺是否在 Promise.all() 完成之前开始控制台日志记录“完成”? Is the .then() for the Promise.all() a separate .then() that will run only once all the promises resolve? Promise.all() 的 .then() 是一个单独的 .then() ,它只会在所有承诺都解决后运行吗?

What is the difference in removing the .then() from all the individual promises and instead executing this code in the .then() of the Promise.all() as shown below?从所有单个 promise 中删除 .then() 而是在 Promise.all() 的 .then() 中执行此代码,如下所示有什么区别? Is there any performance trade off?是否有任何性能权衡?

// assume async() returns a promise that resolves with the number 5. 
promiseArray = [];
for (let i = 0; i < 5; i++){
promiseArray.push(async(5));
}
Promise.all(promiseArray).then((resolves) => resolves.forEach(console.log("done")));

Do the promises start console logging "done" even before Promise.all() completes?承诺是否在 Promise.all() 完成之前开始控制台日志记录“完成”? Is the .then() for the Promise.all() a separate .then() that will run only once all the promises resolve? Promise.all() 的 .then() 是一个单独的 .then() ,它只会在所有承诺都解决后运行吗?

That's right.这是正确的。 The Promises that get pushed to the array all resolve after their done is logged, so then calling Promise.all on the array of Promises will result in all done always being logged after every done has logged.被推送到数组的 Promises它们的done被记录都会解析,因此在 Promises 数组上调用Promise.all将导致all done在每个done被记录后总是被记录。

What is the difference in removing the .then() from all the individual promises and instead executing this code in the .then() of the Promise.all() as shown below?从所有单个 promise 中删除 .then() 而是在 Promise.all() 的 .then() 中执行此代码,如下所示有什么区别? Is there any performance trade off?是否有任何性能权衡?

The main difference is that in the first approach, the done s can be logged as soon as their prior Promise finished .主要区别在于,在第一种方法中, done可以在其先前的 Promise 完成后立即记录。 In contrast, in the second approach:相比之下,在第二种方法中:

Promise.all(promiseArray).then((resolves) => resolves.forEach(

Above, all Promises must resolve before starting to iterate over them again.上面,所有 Promise必须在开始再次迭代之前解析。

This could well be a significant factor.这很可能是一个重要因素。 For example, let's say you have to perform a CPU-intensive calculation for every Promise result.例如,假设您必须为每个 Promise 结果执行 CPU 密集型计算。 In such a case, taking the second approach would definitely be the better choice, because then you can be carrying out some of those calculations while waiting for other Promises to resolve, rather than having to wait for all Promises to resolve before starting the first calculation.在这种情况下,采用第二种方法肯定是更好的选择,因为这样您就可以在等待其他 Promise 解析的同时执行其中的一些计算,而不是在开始第一次计算之前等待所有Promise 解析.

Here's an example where using the second approach is a solid improvement over the first approach.下面是一个示例,其中使用第二种方法比第一种方法有了实质性的改进。 When calculations are expensive, chain them onto the individual Promises , rather than after a Promise.all .当计算成本很高时,将它们链接到单个 Promises 上,而不是在Promise.all

(If the code to be run after individual Promises isn't expensive, then it doesn't matter - you can choose whichever approach you find most readable) (如果要在单个 Promise 之后运行的代码并不昂贵,那么没关系 - 您可以选择您认为最易读的方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM