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.
On this page

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))

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)
}
Console
OK: Leaderboard loaded
Error: Chat unavailable
OK: Badges loaded

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

#
Syntax
Promise.allSettled(iterable)

Parameters

#
  • iterable

    array of promises

    The promises (or plain values) whose settlement you want to wait for.

More examples

#
Shape of the results
JavaScript
const results = await Promise.allSettled([Promise.resolve(10), Promise.reject(new Error('Oops'))])
console.log(results)
Console
[{ status: 'fulfilled', value: 10 }, { status: 'rejected', reason: Error: Oops }]
Only the successful results
JavaScript
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)
Console
['Emma', 'Mia']

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

#

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