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.
Side-by-side comparison
#What it returns on success
- Promise.all()
- An array of results in the order of the promises
- Promise.allSettled()
- An array of
{ status, value }or{ status, reason }objects
When it rejects
- Promise.all()
- Immediately on the first error
- Promise.allSettled()
- Never rejects
Waits for every promise to settle
- Promise.all()
- No, it stops at the first error
- Promise.allSettled()
- Yes, it always waits for all of them
Access to individual promise errors
- Promise.all()
- Only the first error, through
catch() - Promise.allSettled()
- Every error, in the
reasonfield of its result
An empty array as the argument
- Promise.all()
- Fulfills right away with the array
[] - Promise.allSettled()
- Fulfills right away with the array
[]
Typical use
- Promise.all()
- Operations that all must succeed
- Promise.allSettled()
- Independent operations, where one’s error doesn’t affect the rest
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))
try {
await Promise.all([wait(100, 'profile'), fail(150, 'Courses error')])
} catch (error) {
console.log('Promise.all:', error.message)
}
const settled = await Promise.allSettled([wait(100, 'profile'), fail(150, 'Courses error')])
console.log('Promise.allSettled:', settled.map((r) => r.status))Definition and usage
#Both methods take an array of promises and let you run independent operations in parallel rather than one after another, so together they take as long as the slowest one. Both return results in the order of the given promises, regardless of which one settled first.
They differ in how they behave on an error. Promise.all() rejects immediately as soon as a single promise rejects, and the results of the rest are lost (the operations themselves aren’t cancelled, only their result is ignored). Promise.allSettled() never rejects: it waits for every promise to settle and returns an array of { status, value } objects for successes and { status, reason } objects for failures.
When every operation must succeed, and an error in one should abort the whole thing, use Promise.all(). When the operations are independent of each other and you want to know which ones succeeded, even if others failed, use Promise.allSettled().
Syntax
#Promise.all(iterable)
Promise.allSettled(iterable)More examples
#const results = await Promise.allSettled([Promise.resolve('OK'), Promise.reject(new Error('Oops'))])
console.log(results)
try {
await Promise.all([Promise.resolve('OK'), Promise.reject(new Error('Oops'))])
} catch (error) {
console.log(error.message)
}const results = await Promise.allSettled([
Promise.resolve('Ranking'),
Promise.reject(new Error('Chat unavailable')),
Promise.resolve('Badges')
])
const succeeded = results.filter((r) => r.status === 'fulfilled').map((r) => r.value)
console.log(succeeded)Good practices
#- Combine either method with
map()to run the same operation for many elements at once, e.g.await Promise.allSettled(ids.map(loadUser)). - After
Promise.allSettled(), always check thestatusfield of every result. The method itself won’t tell you something went wrong. - When only the first result matters, success or failure, neither of these methods is the right fit. Check out
Promise.race().
Related entries
#- Promise.all()Waits for every promise to be fulfilled and returns an array of their results. Rejects at the first error.
- Promise.allSettled()Waits for every promise to settle and returns a description of each result, both successes and errors.
- 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.any()Returns the result of the first promise to be fulfilled. Rejects only once all of them have failed.
Spotted a mistake or missing an example? Write to us.