Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  1] [ 2]  / answers: 1 / hits: 46138  / 4 Years ago, fri, january 17, 2020, 12:00:00

I was reading the MDN's manual on Promise, and I found these two methods which seem similar to me:





Both of them take an iterable and return an array containing the fulfilled Promises.



So, what is the difference between them?


More From » es6-promise

 Answers
6

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




[#51294] Thursday, January 9, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;