Skip to content

Promises - All Method

Promise.all() takes a list of promises and returns one promise, which is fulfilled when every promise in the list has been fulfilled. Its value is an array of their values, in the same order as the list.

Use it when the pieces of work do not depend on each other but the next step needs all of their results, such as loading a user, their orders and their settings before drawing a page. The pieces run at the same time, so the wait is as long as the slowest one rather than the sum of all of them.

The examples on this page use a small wait() helper, which fulfils a promise with a value after a delay:

const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
const loadUser = () => wait(300, { name: 'Priya' });
const loadOrders = () => wait(100, ['order 41', 'order 42']);
const loadSettings = () => wait(200, { theme: 'dark' });
const start = Date.now();
Promise.all([loadUser(), loadOrders(), loadSettings()]).then(([user, orders, settings]) => {
console.log(`${user.name} has ${orders.length} orders and the ${settings.theme} theme`);
console.log(`Took about ${Math.round((Date.now() - start) / 100) * 100} ms`);
});

This prints:

Priya has 2 orders and the dark theme
Took about 300 ms

The orders arrive first, but the results array still lists them second, because it follows the order of the input rather than the order of completion. That is what makes the array destructuring in the then() callback safe. The whole wait is about 300 milliseconds, which is the time taken by the slowest load.

If any promise in the list is rejected, the promise from Promise.all() is rejected straight away with that reason, without waiting for the others:

const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
const fail = (ms, message) =>
new Promise((resolve, reject) => setTimeout(reject, ms, new Error(message)));
Promise.all([wait(300, 'user'), fail(100, 'Orders service is down'), wait(200, 'settings')])
.then((results) => console.log('All loaded:', results))
.catch((error) => console.log('Failed:', error.message));

This prints:

Failed: Orders service is down

The other two pieces of work are not cancelled, because a promise has no way to stop the work behind it. They finish in the background and their results are discarded. Where every result matters even when some fail, use Promise.allSettled() instead.

The list can contain plain values, which Promise.all() treats as already fulfilled. An empty list gives an empty array:

const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
Promise.all(['plain value', Promise.resolve(2), wait(50, 'later')]).then((results) =>
console.log(results),
);
Promise.all([]).then((results) => console.log(results));

This prints:

[]
[ 'plain value', 2, 'later' ]

Promise.all() accepts any iterable, not only an array, so a Set of promises or the result of map() works too. A common pattern is Promise.all(ids.map(loadItem)), which starts one load per id and waits for all of them.

Steve Griffith covers this topic in his video JavaScript Promises all Method.