In JavaScript, the Promise.allSettled function enables the simultaneous management of several promises and produces a single promise as a result. This resulting promise resolves to an array containing state descriptors, with each one providing insights into the outcome of the respective promise from the provided input array.
In straightforward terms, the Promise.allSettled function allows you to wait until all promises have settled, and it provides an array containing the results of each promise, whether they are fulfilled or rejected.
Syntax
Promise.allSettled(iterable);
Parameters
- iterable: This parameter accepts either an array of promises or a standard array that includes various objects.
- Return value: When the provided argument is empty, the function returns a Promise that is resolved immediately; conversely, in all other scenarios, it yields a pending promise that reflects the status and value of each promise contained within it individually.
Example
const promise1 = Promise.resolve(30);
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'Hello'));
const promises = [promise1, promise2];
Promise.allSettled([promise1, promise2])
.then((results) => results.forEach((result) => console.log(result)));
Output:
{ status: 'fulfilled', value: 30 }
{ status: 'rejected', reason: 'Hello' }
When to Use Promise.allSettled
In JavaScript, you can employ Promise.allSettled when it is essential to guarantee that all of your promises complete, irrespective of whether they resolve successfully or are rejected. This function is especially useful for effectively handling both successful outcomes and errors, as it does not halt execution upon encountering the first rejection.
Examples of Promise.allSettled
Example 1
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'Hello')
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'Bye');
});
Promise.allSettled([promise1, promise2])
.then((results) => results.forEach((result) => console.log(result)));
Output:
{ status: 'fulfilled', value: 'Hello' }
{ status: 'fulfilled', value: 'Bye' }
Explanation
In this snippet, both promises are fulfilled with the values 'Hello' and 'Bye', each after a duration of 1 second. The method Promise.allSettled yields a promise that ultimately resolves to an array of results containing two objects. The first object corresponds to the resolved state of promise1, while the second object pertains to the rejected state of promise2.
Example 2
const p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'okay')
});
const p2 = new Promise((resolve, reject) => {
setTimeout(reject, 2000, 'nothing');
});
Promise.allSettled([p1, p2])
.then((results) => results.forEach((result) => console.log(result)));
Output:
{ status: 'fulfilled', value: 'okay' }
{ status: 'rejected', reason: 'nothing' }
Explanation
In this example, the initial promise p1 successfully resolves to the string ‘okay’ following a duration of one second. Conversely, the subsequent promise p2 fails and rejects with the value ‘nothing’ after a period of two seconds.
The Promise.allSettled method yields a promise that resolves into an array containing two items. The initial item is an object that corresponds to the fulfilled p1 promise, while the subsequent item is another object that is associated with the rejected p2 promise.
Conclusion
The Promise.allSettled function accepts an iterable set of promises and produces a new promise. This newly created promise resolves only after all the input promises have settled, returning an array of objects that outline the results of each promise contained in the iterable.