Promise. allSettled()
Waits for every promise to settle and returns a description of each result, both successes and errors.
- Returns
- A promise that always fulfills with an array of
{ status, value }or{ status, reason }objects.
Example
#const wait = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms))
const fail = (ms, message) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms))
const results = await Promise.allSettled([
wait(200, 'Leaderboard loaded'),
fail(100, 'Chat unavailable'),
wait(300, 'Badges loaded')
])
for (const result of results) {
if (result.status === 'fulfilled') console.log('OK:', result.value)
else console.log('Error:', result.reason.message)
}Definition and usage
#The Promise.allSettled() method works similarly to Promise.all(), but it never rejects because of a single error. It waits until every promise is either fulfilled or rejected, and returns an array of objects describing each one’s outcome.
Each object has a status field. For a success it’s "fulfilled" together with a value field, and for an error it’s "rejected" together with a reason field. The order of the results matches the order of the promises you passed in.
It’s useful when operations are independent of each other and you want to know which ones succeeded: sending several notifications, saving progress in multiple places, or loading widgets that can each fail on their own.
Syntax
#Promise.allSettled(iterable)Parameters
#iterable
array of promises
The promises (or plain values) whose settlement you want to wait for.
More examples
#const results = await Promise.allSettled([Promise.resolve(10), Promise.reject(new Error('Oops'))])
console.log(results)const results = await Promise.allSettled([
Promise.resolve('Emma'),
Promise.reject(new Error('No data')),
Promise.resolve('Mia')
])
const names = results.filter((r) => r.status === 'fulfilled').map((r) => r.value)
console.log(names)Browser support
#Widely available · since 2020
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- After
allSettled(), always check thestatusof each result. The method itself won’t tell you that something went wrong. - If the first error should abort the whole operation,
Promise.all()is the better choice.
Related entries
#- Promise.all() vs Promise.allSettled()Promise.all() rejects at the first error and returns an array of results, while Promise.allSettled() waits for every promise and never rejects.
- Promise.all()Waits for every promise to be fulfilled and returns an array of their results. Rejects at the first error.
- Promise.any()Returns the result of the first promise to be fulfilled. Rejects only once all of them have failed.
- Promise.race()Returns a promise that settles the same way as the first promise in the list to settle, whether with success or an error.
- Promise.prototype.catch()Registers an error-handling function that runs when the promise is rejected.
Spotted a mistake or missing an example? Write to us.