-
Promise.all vs. Promise.allSettledDEV/Javascript 2021. 6. 8. 15:17
From ECMAScript 2022 Language Specification,
The allSettled function returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises have settled, i.e. become either fulfilled or rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise.allsettledThe all function returns a new promise which is fulfilled with an array of fulfillment values for the passed promises, or rejects with the reason of the first passed promise that rejects. It resolves all elements of the passed iterable to promises as it runs this algorithm.
https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise.allThe main difference between those two functions is that Promise.all will immediately stop the execution of the rest of the promises if rejection happens. Some people will be thinking that Promise.all should have been called as 'Promise.allResolved' because it is quite vague for representing its actual role.
In contrast, Promise.allSettled waits until all promises in the array argument settled. This means that a promise can be either resolved or rejected, but it doesn't stop the whole execution of the promises. Because of this, Promise.allSettled function is more useful than Promise.all when you need to investigate the settlement result of an array of promises, not abandoning the whole result.