What does Promise.all() do?
What Promise.all() does
Promise.all() is a method of the Promise class that takes an array (or any iterable) of promises and returns a new promise that:
- fulfills once all the promises inside the array have succeeded (
fulfilled); - rejects, as soon as the first promise fails.
Syntax
Promise.all(iterable)where iterable is usually an array of promises:
const p1 = Promise.resolve('A');
const p2 = Promise.resolve('B');
const p3 = Promise.resolve('C');
Promise.all([p1, p2, p3]).then(values => console.log(values));Result:
["A", "B", "C"]How it works
- All the promises inside
Promise.all([...])start in parallel. (more precisely, they begin executing independently of each other). Promise.all()waits for them to finish.- If all succeed, a new promise in the
fulfilledstate is returned, and its value is an array of results in the same order as the input promises.
Example with delays
const p1 = new Promise(res => setTimeout(() => res('First'), 1000));
const p2 = new Promise(res => setTimeout(() => res('Second'), 2000));
const p3 = new Promise(res => setTimeout(() => res('Third'), 500));
Promise.all([p1, p2, p3])
.then(results => console.log('Results:', results))
.catch(err => console.error('Error:', err));Output after about 2 seconds:
Results: [ 'First', 'Second', 'Third' ]Notice: the results are in the same order the promises were passed in, not in the order they finished.
What happens if one of the promises rejects
If at least one promise inside Promise.all transitions to the rejected state,
the whole Promise.all() immediately rejects,
and the result is the first caught error.
Example:
const p1 = Promise.resolve('OK');
const p2 = Promise.reject('Error!');
const p3 = Promise.resolve('Also OK');
Promise.all([p1, p2, p3])
.then(results => console.log(results))
.catch(err => console.error(err));Output:
Error!The rest of the promises (p3) still run,
but their results are ignored,
because Promise.all() has already transitioned to the rejected state.
Important: results are kept in the order they were passed
Even if one promise takes longer to resolve, the results are ordered by position:
Promise.all([
Promise.resolve('A'),
new Promise(res => setTimeout(() => res('B'), 2000)),
Promise.resolve('C')
]).then(console.log); // ["A", "B", "C"]If you pass non-promises
Promise.all() automatically wraps plain values in Promise.resolve():
Promise.all([42, Promise.resolve('OK')])
.then(console.log); // [42, "OK"]How to handle errors without interrupting execution
If you need all promises to run independently,
and errors should not interrupt execution,
use Promise.allSettled().
Promise.allSettled([
Promise.resolve('OK'),
Promise.reject('Error'),
Promise.resolve('Also OK')
]).then(console.log);Output:
[
{ status: "fulfilled", value: "OK" },
{ status: "rejected", reason: "Error" },
{ status: "fulfilled", value: "Also OK" }
]Promise.allSettled() never rejects,
and returns an array of results with status and value/reason fields.
A simple analogy
Imagine you're waiting for 3 deliveries:
Promise.all(): you wait for all the packages at once. If even one gets lost, you cancel everything.Promise.allSettled(): you wait for all of them, even if one gets broken, you still get a report on each.
SUMMARY
| Method | What it does | Success | Error |
|---|---|---|---|
Promise.all() | Waits for all promises to fulfill | Returns an array of results | Rejects on the first error |
Promise.allSettled() | Waits for all, even with errors | Returns an array of {status, value/reason} objects | - |
Promise.race() | Returns the result of the first promise to settle | Can be a success or an error | - |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.