Fulfilled or resolve (for successful operations)
Rejected (for failure operation)
Neither fulfilled nor rejected
The methods implemented with the Promise class in ES6 or ES2015 are
resolve, etc. While the
allSettled() method is introduced in ECMAScript 2020.
Promise.allSettled() method accepts a list of promises and returns a new promise which resolves after all the input promises have settled, either resolved or rejected.
In simple words, we can say that this function can be used to monitor and report the status of a list of promises.
This method is used when we have multiple asynchronous tasks. These asynchronous tasks are not dependent on each other to complete successfully. This method can also be used when we want to know the result of each Promise.
In comparison, the Promise returned by
Promise.all() is used when the tasks are dependent on each other.
Here is how we can use this function,
- An iterable is an object such as an array in which each member is a promise.
When we pass an empty iterable as an argument,
Promise.allSettled() method returns a Promise object which has already been resolved as an empty array.
For each returned object, there is a status string. If the value of the status attribute is fulfilled, then the value attribute is present, and if the value of the status attribute is rejected, then there is a reason attribute.
The value (or reason) attribute determines whether each Promise was fulfilled (or rejected).
In the given example we have used the
Promise.allSettled() method to wait for all the input promises to settle.
As we can see in the output image, the first Promise p1 fulfilled the value 10 after 1 second, and the promise p2 was rejected to a reason 20 after 2 seconds.
We have learned about the
Promise.allSettled() method introduced in ECMAScript 2020. This method accepts a list of promises and returns a new promise, that resolves once every input Promise has settled, either rejected or fulfilled.