ComparisonComparisons

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.

On this page

Side-by-side comparison

#

Example

#
JavaScript
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))
Console
Promise.all: Courses error
Promise.allSettled: ['fulfilled', 'rejected']

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

#
Syntax
Promise.all(iterable)
Promise.allSettled(iterable)

More examples

#
The shape of allSettled() results vs the error from all()
JavaScript
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)
}
Console
[{ status: 'fulfilled', value: 'OK' }, { status: 'rejected', reason: Error: Oops }]
Oops
Only the successful results from allSettled()
JavaScript
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)
Console
['Ranking', 'Badges']

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 the status field 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

#

Spotted a mistake or missing an example? Write to us.