Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.all

Waits for all promises in parallel.

Available since: ES2015

Syntax

Promise.all(promises)

Parameters

ParameterTypeDescription
promisesIterableAn array of promises (or plain values).

Return value

PromiseA promise of an array of results in the same order — or the first error.

Examples

Loading in parallel:

const wait = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));
 
const [user, orders] = await Promise.all([wait(100, 'Anna'), wait(50, 3)]);
 
console.log(user, orders);

Output:

Anna 3

One error rejects the whole Promise.all:

try {
  await Promise.all([Promise.resolve(1), Promise.reject(new Error('Failed')), Promise.resolve(3)]);
} catch (error) {
  console.log(error.message);
}

Output:

Failed

Notes

Results come in the order of the promises, not the order they finished. The total time equals the slowest promise, not the sum.

If you need the results even when some fail, use Promise.allSettled().

See also

  • Promise.allSettled — Waits for every promise to settle and reports each result — success or failure.
  • Promise.race — Returns the result of the first promise to settle — successful or not.
  • Promise.any — Returns the first successful result and skips errors.
  • await — Waits for a promise and returns its value.

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.