Promise.all(iterable) runs nothing itself. It waits for several already-started async operations in parallel and returns one promise that fulfils with an array of results in input order once all of them fulfil. It rejects as soon as any one rejects (fail-fast), with that first reason. Use it when the operations are independent and you need every result to continue.
async function loadDashboard(userId) {
const [user, orders, notifications] = await Promise.all([
fetchJson(`/api/users/${userId}`),
fetchJson(`/api/users/${userId}/orders`),
fetchJson(`/api/notifications`),
]);
return { user, orders, notifications };
}
Total time ≈ the slowest request, instead of the sum of all three if you awaited them one by one:
// Sequential: ~ t1 + t2 + t3
const user = await fetchJson(a);
const orders = await fetchJson(b);
// Parallel: ~ max(t1, t2, t3)
const [user2, orders2] = await Promise.all([fetchJson(a), fetchJson(b)]);
Promise.all([1, fetchX()]) treats 1 as already fulfilled.[].Promise.all(ids.map(load)) or a Set of promises.Because fail-fast leaves other requests running, pass a shared signal and abort the rest when one fails:
async function loadAll(urls) {
const controller = new AbortController();
try {
return await Promise.all(
urls.map((u) => fetch(u, { signal: controller.signal }).then((r) => {
if (!r.ok) throw new Error(`HTTP ${r.status} for ${u}`);
return r.json();
})),
);
} catch (err) {
controller.abort(); // stop the stragglers
throw err;
}
}
Promise.all(hugeList.map(upload)) starts everything at once, which can overwhelm the server, hit browser connection limits or rate limits. Process in a pool instead:
async function mapWithLimit(items, limit, fn) {
const results = new Array(items.length);
let next = 0;
async function worker() {
while (next < items.length) {
const i = next++;
results[i] = await fn(items[i], i);
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
return results;
}
| Need | Use |
|---|---|
| Every outcome, even if some fail (e.g. batch upload report) | Promise.allSettled |
| First success among redundant sources (mirrors, CDNs) | Promise.any |
| First to settle, e.g. a timeout race | Promise.race (or AbortSignal.timeout) |
| Each step depends on the previous result | Sequential await |
for (const id of ids) results.push(await load(id)) is serial. Build the array of promises first, then await Promise.all.forEach with async callbacks doesn't wait for anything. Use map + Promise.all.Promise.all's internal subscription, so no warning. But side effects from those operations still happen.allSettled for independent UI sections.AbortController fixes that.allSettled/any/race fit better.Spotted a mistake or a rights concern? Report an issue