Promise.race and Promise.allSettled help you handle multiple tasks that happen at the same time. They let you decide what to do when some or all tasks finish.
Promise.race and Promise.allSettled in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
Promise.race(iterableOfPromises) Promise.allSettled(iterableOfPromises)
Promise.race returns a promise that settles as soon as one of the promises settles (either fulfilled or rejected).
Promise.allSettled returns a promise that resolves after all promises have settled, with an array of their results.
Promise.race([promise1, promise2, promise3])
Promise.allSettled([promise1, promise2, promise3])
This code creates three promises: one resolves after 300ms, one rejects after 200ms, and one resolves after 400ms.
Promise.race returns the first promise that settles, which is the rejected one after 200ms.
Promise.allSettled waits for all promises to finish and then shows their status and values or reasons.
const promise1 = new Promise(resolve => setTimeout(() => resolve('First done'), 300)); const promise2 = new Promise((_, reject) => setTimeout(() => reject('Second failed'), 200)); const promise3 = new Promise(resolve => setTimeout(() => resolve('Third done'), 400)); // Using Promise.race Promise.race([promise1, promise2, promise3]) .then(result => console.log('Race result:', result)) .catch(error => console.log('Race error:', error)); // Using Promise.allSettled Promise.allSettled([promise1, promise2, promise3]) .then(results => { console.log('AllSettled results:'); results.forEach((res, i) => { console.log(`Promise ${i + 1}:`, res.status, res.status === 'fulfilled' ? res.value : res.reason); }); });
Promise.race settles as soon as any promise settles, so it can reject or resolve first.
Promise.allSettled never rejects; it always resolves with the status of all promises.
Use Promise.allSettled when you want to know the outcome of all promises without stopping on errors.
Promise.race gives you the first finished promise result or error.
Promise.allSettled waits for all promises and tells you how each ended.
Both help manage multiple tasks running at the same time in a simple way.
Practice
Promise.race do when given multiple promises?Solution
Step 1: Understand Promise.race behavior
Promise.race returns as soon as any promise settles (resolves or rejects).Step 2: Compare with other Promise methods
Unlike Promise.all or allSettled, it does not wait for all promises.Final Answer:
Returns the result or error of the first promise that finishes. -> Option DQuick Check:
Promise.race = first finished promise result [OK]
- Thinking it waits for all promises
- Assuming it only returns successful results
- Believing it cancels other promises
Promise.allSettled with an array of promises named tasks?Solution
Step 1: Recall Promise.allSettled usage
Promise.allSettled returns a promise that resolves with an array of results.Step 2: Check correct method chaining
We use .then() to handle the resolved results, not .catch() or .resolve().Final Answer:
Promise.allSettled(tasks).then(results => console.log(results)); -> Option CQuick Check:
Use .then() to get allSettled results [OK]
- Using .catch() instead of .then() for results
- Trying to use .resolve() method on promise
- Assuming .finally() receives results
const p1 = new Promise(res => setTimeout(() => res('A'), 100));
const p2 = new Promise((_, rej) => setTimeout(() => rej('Error'), 50));
Promise.race([p1, p2])
.then(console.log)
.catch(console.error);What will be printed?
Solution
Step 1: Identify which promise settles first
p2 rejects after 50ms, p1 resolves after 100ms, so p2 finishes first.Step 2: Understand Promise.race behavior on rejection
Promise.race rejects immediately with the first rejection, so .catch logs 'Error'.Final Answer:
'Error' -> Option AQuick Check:
First finished promise is rejection 'Error' [OK]
- Assuming resolve wins over reject
- Expecting an array instead of single result
- Thinking code throws syntax error
const promises = [Promise.resolve(1), Promise.reject('fail')];
Promise.allSettled(promises).catch(console.error);Solution
Step 1: Recall Promise.allSettled behavior
It waits for all promises and never rejects, it always resolves with results.Step 2: Understand .catch usage here
Since it never rejects, .catch will never be called, so error handling is ineffective.Final Answer:
Promise.allSettled never rejects, so .catch will never run. -> Option BQuick Check:
allSettled always resolves, .catch unused [OK]
- Thinking allSettled rejects on any promise failure
- Believing async/await is required
- Assuming .then() must come before .catch() always
task1, task2, and task3. You want to get the first task that finishes successfully, but if all fail, you want to know all errors. Which approach correctly achieves this?Solution
Step 1: Understand requirement for first success or all errors
Promise.race gives first finished promise, but may reject if first is failure.Step 2: Combine Promise.race and Promise.allSettled
If Promise.race rejects, then run Promise.allSettled to collect all errors from all tasks.Step 3: Evaluate other options
Promise.allSettled alone waits for all, no early success; Promise.all fails fast; ignoring errors loses info.Final Answer:
Use Promise.race on all tasks, then if it rejects, run Promise.allSettled to get all errors. -> Option AQuick Check:
race first success, allSettled for all errors [OK]
- Using allSettled only and waiting too long
- Ignoring rejected promises in race
- Using all which fails on first rejection
