You will be able to run independent asynchronous JavaScript operations at the same time using Promise.all, and choose safer alternatives when some operations may fail.
What it is
Parallel requests mean starting several asynchronous operations before waiting for their results. In JavaScript, functions that return promises can overlap: the event loop can send multiple network requests, then resume when responses arrive. This is usually called concurrency, not true CPU parallelism, because JavaScript runs your code on one thread.
The common pattern is to create promises first, then wait for them together. Promise.all takes an array of promises and returns one promise that resolves to an array of results in the same order. Related terms include async/await, promise rejection, fail-fast, Promise.allSettled, and Promise.race.
Why it matters
- It reduces total waiting time when requests are independent.
- It keeps code concise compared with manually tracking multiple callbacks.
- It makes loading page data, such as user profile, orders, and notifications, feel faster.
- It helps you reason about ordering: results appear in the same order as the input array.
- It gives a clear place to handle success or failure for a group of operations.
Syntax or steps
const [a, b] = await Promise.all([f1(), f2()]);
- Call each asynchronous function to create a promise, such as
f1()andf2(). - Put those promises in an array.
- Pass the array to
Promise.all. - Await the returned promise.
- Destructure the result array into variables.
Example
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function getUser(id) {
await delay(300);
return { id, name: `User ${id}` };
}
async function loadUsers() {
const [user1, user2, user3] = await Promise.all([
getUser(1),
getUser(2),
getUser(3)
]);
console.log(user1, user2, user3);
}
loadUsers();
Part-by-part explanation
delaycreates a promise that resolves after a timeout, simulating a network request.getUseris anasyncfunction, so it returns a promise.Promise.all([getUser(1), getUser(2), getUser(3)])starts all three calls before waiting.awaitpausesloadUsersuntil all three promises resolve successfully.- The results are destructured in the same order as the input array.
Common mistakes
- Writing
await getUser(1); await getUser(2);for independent requests. Fix: usePromise.all([getUser(1), getUser(2)]). - Assuming
Promise.allwaits for every request to finish. It rejects as soon as one promise rejects. Fix: usePromise.allSettledwhen partial failure is acceptable. - Passing functions instead of calling them, such as
Promise.all([getUser, getUser]). Fix: call the functions to create promises. - Starting hundreds of requests at once. Fix: limit concurrency with batching or a queue.
When to use it
Use Promise.all when operations are independent and all must succeed. If one operation depends on another, run them sequentially. If some operations may fail but you still want the others, use Promise.allSettled.
| Pattern | Behavior | Use when |
|---|---|---|
await a(); await b(); | Runs one after another | Operations depend on previous results or must be ordered |
Promise.all([a(), b()]) | Runs together; fails fast | Independent operations where all must succeed |
Promise.allSettled([a(), b()]) | Runs together; reports each outcome | Independent operations where partial failure is acceptable |
Practice
Guided exercise: Write a function that calls getUser(1) and getUser(2) in parallel and logs both users. Expected output: two user objects printed after about 300 ms, not 600 ms.
Challenge: Modify the example so one request fails and the code still logs the successful result. Hint: use Promise.allSettled and inspect each result's status and value or reason.
Quick check
Question: If you write await getUser(1); await getUser(2);, are the requests parallel?
Answer: No. The second request starts only after the first finishes. Use Promise.all([getUser(1), getUser(2)]) to start them together.
Summary
Parallel requests in JavaScript are created by starting multiple promises and then waiting for them together. Promise.all is the simplest fail-fast pattern for independent operations that must all succeed.