admin管理员组文章数量:1134247
I was reading the MDN's manual on Promise, and I found these two methods which seem similar to me:
- Promise.allSettled(iterable);
- Promise.all(iterable);
Both of them take an iterable and return an array containing the fulfilled Promise
s.
So, what is the difference between them?
I was reading the MDN's manual on Promise, and I found these two methods which seem similar to me:
- Promise.allSettled(iterable);
- Promise.all(iterable);
Both of them take an iterable and return an array containing the fulfilled Promise
s.
So, what is the difference between them?
Share Improve this question edited Jan 17, 2020 at 9:27 VLAZ 28.8k9 gold badges62 silver badges82 bronze badges asked Jan 17, 2020 at 9:07 ezio4dfezio4df 4,1076 gold badges19 silver badges32 bronze badges 5 |4 Answers
Reset to default 272Promise.all
will reject as soon as one of the Promises in the array rejects.
Promise.allSettled
will never reject - it will resolve once all Promises in the array have either rejected or resolved.
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.allSettled
will instead give you [{ 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.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.allSettled: This one will always get resolved with an array having info about resolved and rejected promises. Have a close look at following properties (status, value, reason ) of resulting array.
Example 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:
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.
But when you just want to complete all the promises irrespective to whether they are resolved or rejected then use Promise.allSettled
.
Both of them execute promises in bulk but the subtle difference is the way they are handling the promise iterations.
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.allSettled : It returns a promise which resolves when all the promises in the array are settled (rejected or resolved).
Note : Both of them take an iterable and return an array containing the fulfilled Promises.
本文标签: javascriptDifferences between Promiseall() and PromiseallSettled() in JSStack Overflow
版权声明:本文标题:javascript - Differences between Promise.all() and Promise.allSettled() in JS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736802699a1953554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
all
is executed for resolved promises,allSettled
is any that are finished - resolved and rejected. The first sentence of each article outlines this difference. – VLAZ Commented Jan 17, 2020 at 9:08.all
the promise returned does not always resolve to an array - it's right there in the first line of the documentation - in fact, read the first paragraph of each of the documentation you linked to, and you have your answer – Jaromanda X Commented Jan 17, 2020 at 10:25