How do asynchronous (parallel) API calls improve server performance, and what does Promise.all() do?
Firing independent requests in parallel means total time equals the slowest request, not the sum of all — and Promise.all() is the tool that launches them together and waits for all to finish.
The key move is to start every request before awaiting any of them, so they run in the background simultaneously:
// Sequential (slow): each await blocks the next
const a = await fetch(api1);
const b = await fetch(api2);
const c = await fetch(api3);
// Parallel (fast): all three fly at once, await the whole set
const [a, b, c] = await Promise.all([
fetch(api1),
fetch(api2),
fetch(api3)
]);
With the three calls from the previous card (200/300/250 ms), the sequential version takes ~750 ms but the parallel version takes ~300 ms — the duration of the slowest one. While the requests are in flight the server isn't blocked and can do other work.
Only use Promise.all() when the requests don't depend on each other. Note its fail-fast behaviour: if any one promise rejects, Promise.all() rejects immediately — use Promise.allSettled() if you want every result regardless of individual failures.
Go deeper:
Promise.all() — MDN — runs promises concurrently and resolves when all finish; note the fail-fast behaviour if one rejects.