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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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));
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