Promises are one of the very powerful feature of JavaScript which were introduced in ES2015 and very well adopted by the developers community. To enhance the capabilities of Promises, ECMAScript introduced an additional method to the Promise object, allSettled in ES2020.

The allSettled method of Promise object takes an Array of promises as its input and returns an array of Objects once all the passed promises are settled (either resolved or rejected) . Each object in the returned array contains status and value/reason properties.

If the input promise is resolved, then the corresponding return object will have the properties ‘status‘ with ‘fulfilled’ value and ‘value‘ with the value returned from the promise. If the input promise gets rejected, then its corresponding returned object’s status value will be ‘rejected’ and ‘reason’ will be the reason for the rejection (error message).

example:

let promise1 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('Resolved after 1sec')
    }, 1000);
});

let promise2 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('Resolved after 2sec')
    }, 5000);
})

let promise3 = new Promise((resolve, reject) => {
    setTimeout(() => {
        reject('Rejected after 3sec')
    }, 3000);
});

let resultPromise = Promise.allSettled([promise1, promise2, promise3]);

resultPromise.then((data) => {
    console.log(data);
})

Output:

[
  { status: 'fulfilled', value: 'Resolved after 1sec' },
  { status: 'fulfilled', value: 'Resolved after 2sec' },
  { status: 'rejected', reason: 'Rejected after 3sec' }
]

Never Rejects:

The Promise returned from the  allSettled method never gets rejected. It will always resolve with an array containing data about the resolved/rejected promises.

With Await:

Since allSettled method returns a promise, we can use it with async/await just like any other promise.

example:

let promise1 = Promise.resolve('Resolved Promise')

let promise2 = Promise.reject('Rejected Promise');

async function processPromises() {
    let result = await Promise.allSettled([promise1, promise2]);
    console.log(result);
}

processPromises()

Output:

[
  { status: 'fulfilled', value: 'Resolved Promise' },
  { status: 'rejected', reason: 'Rejected Promise' }
]