Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.allSettled

Waits for every promise to settle and reports each result — success or failure.

Available since: ES2020

Syntax

Promise.allSettled(promises)

Parameters

ParameterTypeDescription
promisesIterableAn array of promises.

Return value

PromiseA promise of an array of objects with status and value (or reason).

Examples

Results together with errors:

const results = await Promise.allSettled([
  Promise.resolve('ok'),
  Promise.reject(new Error('Failed')),
]);
 
for (const result of results) {
  console.log(result.status, result.value ?? result.reason.message);
}

Output:

fulfilled ok
rejected Failed

Only successful values:

const results = await Promise.allSettled([Promise.resolve(1), Promise.reject(new Error('x')), Promise.resolve(3)]);
const values = results.filter((r) => r.status === 'fulfilled').map((r) => r.value);
 
console.log(values.join(', '));

Output:

1, 3

Notes

It never rejects: each error just ends up in its own object with the rejected status.

See also

  • Promise.all — Waits for all promises in parallel.
  • Promise.any — Returns the first successful result and skips errors.

Related recipes

In the tutorial

  • Several promises at once — Promise.all, Promise.allSettled, Promise.race and Promise.any in JavaScript: how to wait for several asynchronous actions at the same time.

Thanks for reading!

Donate to the site — we work hard for you.