Skip to content

Promises - AllSettled Method

Promise.allSettled() takes a list of promises and waits until every one of them has settled, whether fulfilled or rejected. It returns a promise that is always fulfilled, with an array holding one result object per input promise, in the input order.

Use it when each piece of work stands alone and a failure in one should not hide the results of the others, such as running a set of health checks or saving several independent records. Promise.all() is the better choice when the work is only useful if every part succeeds.

Each result object has a status property, which is either 'fulfilled' or 'rejected'. A fulfilled result also has a value property, and a rejected one has a reason property instead:

const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
const fail = (ms, message) =>
new Promise((resolve, reject) => setTimeout(reject, ms, new Error(message)));
const runChecks = () => [
wait(100, 'Database OK'),
fail(50, 'Cache unreachable'),
wait(150, 'Queue OK'),
];
Promise.allSettled(runChecks()).then((results) => {
results.forEach((result) => {
if (result.status === 'fulfilled') {
console.log('fulfilled:', result.value);
} else {
console.log('rejected: ', result.reason.message);
}
});
});

This prints:

fulfilled: Database OK
rejected: Cache unreachable
fulfilled: Queue OK

The same checks passed to Promise.all() give only the first failure:

Promise.all(runChecks())
.then((values) => console.log('all():', values))
.catch((error) => console.log('all() rejected:', error.message));

This prints:

all() rejected: Cache unreachable

runChecks() is a function so that each example starts a fresh set of checks.

Because the result objects share one shape, filter() and map() can split them into two lists:

Promise.allSettled(runChecks()).then((results) => {
const passed = results.filter((r) => r.status === 'fulfilled').map((r) => r.value);
const failed = results.filter((r) => r.status === 'rejected').map((r) => r.reason.message);
console.log('Passed:', passed);
console.log('Failed:', failed);
});

This prints:

Passed: [ 'Database OK', 'Queue OK' ]
Failed: [ 'Cache unreachable' ]
  • Because the returned promise is never rejected by a failed input, a catch() after it does not see those failures. Check each status instead.
  • It waits for the slowest promise, even after an early failure, so a promise that never settles holds it up for ever.
  • Promise.allSettled() was added in ES2020 and works in all current browsers and Node.

Steve Griffith covers this topic in his video Promises and the allSettled Method.