JS Async Parallel
In this page:
const results = await Promise.all([promise1, promise2]);
await Promise.allSettled([promise1, promise2]);
await Promise.race([promise1, promise2]);
The Problem with Sequential Awaiting
Awaiting one async operation, then starting and awaiting the next, and so on, means the TOTAL time is the SUM of every individual operation's time -- if three independent operations each take one second, sequential awaiting takes three seconds total, even though none of them actually depended on each other.
Example: The Problem with Sequential Awaiting
async function sequential() {
const a = await new Promise(r => setTimeout(() => r(1), 1000));
const b = await new Promise(r => setTimeout(() => r(2), 1000));
console.log(a, b); // takes ~2 seconds total
}
sequential();
Running in Parallel with Promise.all()
Promise.all([promise1, promise2, ...]) starts every promise in the array immediately (they run concurrently, not one after another), and resolves once all of them have succeeded, with an array of their results in the same order they were passed in.
Example: Running in Parallel with Promise.all()
async function parallel() {
const [a, b] = await Promise.all([
new Promise(r => setTimeout(() => r(1), 1000)),
new Promise(r => setTimeout(() => r(2), 1000)),
]);
console.log(a, b); // takes ~1 second total
}
parallel();
Handling Partial Failures with Promise.allSettled()
Unlike Promise.all(), Promise.allSettled() always waits for every promise to finish, regardless of individual success or failure -- each result in the returned array has a status field ("fulfilled" or "rejected") plus either a value or a reason, letting you handle mixed outcomes gracefully.
Example: Handling Partial Failures with Promise.allSettled()
// Wait for every promise to settle (succeed or fail) before continuing
Promise.allSettled([
Promise.resolve(1), // resolves immediately with the value 1
Promise.reject("failed"), // rejects immediately with the reason "failed"
]).then(results => console.log(results)); // results holds a {status, value/reason} object per promise
Racing Operations with Promise.race()
Promise.race([promise1, promise2, ...]) settles (resolves or rejects) as soon as the FIRST promise in the array settles, ignoring whatever the others eventually do -- commonly used to implement a timeout, racing a real operation against a promise that rejects after a fixed delay.
Example: Racing Operations with Promise.race()
// Race two promises; whichever settles first wins, the other is ignored
Promise.race([
new Promise(r => setTimeout(() => r("data"), 100)), // resolves with "data" after 100ms
new Promise((_, reject) => setTimeout(() => reject("timeout"), 50)), // rejects with "timeout" after 50ms
]).then(console.log).catch(console.log); // the faster one (the 50ms rejection) wins the race
Choosing the Right Combinator
Promise.all() is right when every operation must succeed for the overall result to be meaningful; Promise.allSettled() is right when partial success is acceptable and you want visibility into every outcome; Promise.race() is right for a "whichever finishes first" scenario like a timeout.
Example: Choosing the Right Combinator
Promise.all([Promise.resolve(1), Promise.resolve(2)]).then(r => console.log("all:", r));
Promise.allSettled([Promise.resolve(1), Promise.reject("x")]).then(r => console.log("settled:", r));
- Awaiting several independent promises sequentially with separate await statements, one after another, when starting them all at once and awaiting together would take a fraction of the time.
- Using Promise.all() when one of the operations might fail, without realizing it rejects immediately as soon as ANY promise rejects, potentially losing the results of the others that already succeeded.
- Confusing Promise.all() (fails fast on any rejection) with Promise.allSettled() (waits for everything, reporting both successes and failures).
- Promise.all(arrayOfPromises) runs multiple promises in parallel, resolving once ALL of them succeed, or rejecting as soon as ANY of them fails.
- Promise.allSettled(arrayOfPromises) also runs in parallel, but always waits for every promise to finish, reporting each one's individual success or failure.
- Promise.race() resolves or rejects as soon as the FIRST promise in the array settles, whichever that is.
Promise.all() has been supported since Promises were introduced; Promise.allSettled() and Promise.any() are supported in all current major browsers.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic