简体   繁体   English

JS 中 Promise.all() 和 Promise.allSettled() 的区别?

[英]Differences between Promise.all() and Promise.allSettled() in JS?

I was reading the MDN 's manual on Promise , and I found these two methods which seem similar to me:我正在阅读MDN关于Promise的手册,我发现这两种方法看起来与我相似:

Both of them take an iterable and return an array containing the fulfilled Promise s.它们都采用一个可迭代对象并返回一个包含已实现的Promise的数组。

So, what is the difference between them?那么,它们之间有什么区别呢?

Promise.all will reject as soon as one of the Promises in the array rejects.一旦数组中的一个Promise 被拒绝, Promise.all将立即拒绝。

Promise.allSettled will never reject - it will resolve once all Promises in the array have either rejected or resolved. Promise.allSettled永远不会拒绝 - 一旦数组中的所有Promise 都被拒绝或解决,它就会解决。

Their resolve values are different as well.它们的解析值也不同。 Promise.all will resolve to an array of each of the values that the Promises resolve to - eg [Promise.resolve(1), Promise.resolve(2)] will turn into [1, 2] . Promise.all将解析为 Promise 解析为的每个值的数组 - 例如[Promise.resolve(1), Promise.resolve(2)]将变成[1, 2] Promise.allSettled will instead give you [{ status : 'fulfilled', value: 1 }, { status : 'fulfilled', value: 2 }] . Promise.allSettled会给你[{ status : 'fulfilled', value: 1 }, { status : 'fulfilled', value: 2 }]

 Promise.all([Promise.resolve(1), Promise.resolve(2)]) .then(console.log); Promise.allSettled([Promise.resolve(1), Promise.resolve(2)]) .then(console.log);

If one of the Promises rejects, the Promise.all will reject with a value of the rejection, but Promise.allSettled will resolve with an object of { status: 'rejected', reason: <error> } at that place in the array.如果其中一个 Promise 被拒绝, Promise.all将使用拒绝值拒绝,但Promise.allSettled将使用数组中该位置的{ status: 'rejected', reason: <error> }对象解析。

 Promise.all([Promise.reject(1), Promise.resolve(2)]) .catch((err) => { console.log('err', err); }); Promise.allSettled([Promise.reject(1), Promise.resolve(2)]) .then(console.log);

Promise.all: It resolves only when all promises passed to it ( as an array) resolves else it will reject with the first rejected promise error. Promise.all:只有当所有传递给它的 Promise(作为一个数组)都解决时才会解决,否则它将拒绝第一个被拒绝的 Promise 错误。

Promise.allSettled: This one will always get resolved with an array having info about resolved and rejected promises. Promise.allSettled:这个总是会通过一个包含已解决已拒绝承诺信息的数组来解决。 Have a close look at following properties (status, value, reason ) of resulting array.仔细查看结果数组的以下属性(状态、值、原因)。

--------------------------------------------------------- Example 1 ----------------------------------------------------------- -------------------------------------------------- ------- 示例 1 ----------------------------------------- ------------------

const pms1 = Promise.resolve(1);
// setTimeout(function, milliseconds, param1, param2, ...)
const pms2 = new Promise((resolve, reject) => { setTimeout(resolve, 200, 2); });
const pms3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 3); });
const pmsAry = [pms1, pms2, pms3];

Promise.all(pmsAry)
.then(resAry => console.log(resAry)) // resAry order is same as pmsAry order
.catch(error => console.log(error));

/* 
 * Note here we are not writing 'catch' because Promise.allSettled ALWAYS RESOLVES
 * with array containing information about resolved or rejected promises
 */
Promise.allSettled(pmsAry)
.then(resAry => console.log(resAry)); // resAry order is same as pmsAry order

Output :输出 :

[1, 2, 3] 
// Promise.all output ORDER doesn't depend on promise resolution time

[{ status: "fulfilled", value: 1 },
 { status: "fulfilled", value: 2 }, 
 { status: "fulfilled", value: 3 }]
// Promise.allSettled output ORDER doesn't depend on promise resolution time

--------------------------------------------------------- Example 2 ----------------------------------------------------------- -------------------------------------------------- -------- 示例 2 ----------------------------------------- ------------------

const pms1 = Promise.resolve(1);
const pms2 = new Promise(
                 (resolve, reject) => { setTimeout(reject, 200, '200ms Err'); }
             );
const pms3 = new Promise(
                 (resolve, reject) => { setTimeout(reject, 100, '100ms Err'); }
             );
const pmsAry = [pms1, pms2, pms3];

Promise.all(pmsAry)
.then(resAry => console.log(resAry))
.catch(error => console.log(error));

Promise.allSettled(pmsAry)
.then(resAry => console.log(resAry));

Output :输出 :

100ms Err
/* 
 * Note: Here there are TWO promises which are getting REJECTED but output is
 * ONLY ONE (i.e the one which is getting rejected FIRST) 
 */

[{ status: "fulfilled", value: 1 },             // Note: value
 { status: "rejected", reason: "200ms Err" },   
 { status: "rejected", reason: "100ms Err" }]   // Note: reason

When you want to make sure that the promise should all be resolved/success for the operation you are using then you need to use Promise.all since it completes when it get resolved for each of the promise.当您想确保您正在使用的操作的承诺应该全部解决/成功时,您需要使用Promise.all因为它在每个承诺得到解决时完成。

But when you just want to complete all the promises irrespective to whether they are resolved or rejected then use Promise.allSettled .但是,当您只想完成所有承诺时,无论它们是否已解决或被拒绝,请使用Promise.allSettled

Both of them execute promises in bulk but the subtle difference is the way they are handling the promise iterations.它们都批量执行 Promise,但细微的区别在于它们处理 Promise 迭代的方式。

Promise.all : It returns a promise which resolves, when all promises from an array are resolved and gets rejected if one or more promises get rejected. Promise.all :它返回一个解决的承诺,当来自数组的所有承诺都被解决时,如果一个或多个承诺被拒绝,则被拒绝。


Promise.allSettled : It returns a promise which resolves when all the promises in the array are settled (rejected or resolved). Promise.allSettled :它返回一个 Promise,当数组中的所有 Promise 都已解决(被拒绝或已解决)时,该 Promise 将解决。


Note : Both of them take an iterable and return an array containing the fulfilled Promises.注意:它们都采用一个可迭代对象并返回一个包含已实现 Promises 的数组。

在此处输入图像描述

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

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