Promise.all

Wait for several promises to finish together.

Syntaxconst results = await Promise.all([p1, p2]);

Promise.all() takes an array of promises and resolves once all of them have resolved, giving you an array of their results.

Why use it

It runs asynchronous tasks in parallel and waits for the whole group, instead of awaiting each one in sequence.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Fetch a user's profile, orders, and notification count in parallel and render the dashboard once all three arrive.
  • Upload multiple files concurrently with Promise.all instead of uploading them sequentially.
  • Use Promise.allSettled to run several independent API calls and handle each result even if some fail.

More examples

Promise.all for parallel fetches

Runs two fetch calls in parallel and destructures the resolved values once both complete.

Example · js
async function loadDashboard() {
  const [user, orders] = await Promise.all([
    fetch("/api/user/1").then(r => r.json()),
    fetch("/api/orders?userId=1").then(r => r.json())
  ]);
  console.log(user.name, orders.length);
}

Promise.allSettled for fault tolerance

Uses Promise.allSettled to process all promises regardless of failures, logging each status.

Example · js
const results = await Promise.allSettled([
  Promise.resolve("data A"),
  Promise.reject(new Error("B failed")),
  Promise.resolve("data C")
]);
results.forEach(r => console.log(r.status, r.value ?? r.reason?.message));

Promise.race for timeout guard

Uses Promise.race to reject with a timeout error if the fetch does not complete within 3 seconds.

Example · js
function withTimeout(promise, ms) {
  const timeout = new Promise((_, reject) =>
    setTimeout(() => reject(new Error("Timed out")), ms));
  return Promise.race([promise, timeout]);
}
withTimeout(fetch("/api/data"), 3000)
  .catch(e => console.error(e.message));

Discussion

  • Be the first to comment on this lesson.
Promise.all — JavaScript | SoundsCode