Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Promise.race and Promise.allSettled in Node.js
📖 Scenario: You are building a Node.js app that fetches data from multiple sources. Sometimes, you want to get the fastest response, and other times you want to know the result of all requests, whether they succeed or fail.
🎯 Goal: Learn how to use Promise.race to get the first completed promise and Promise.allSettled to get the results of all promises regardless of success or failure.
📋 What You'll Learn
Create three promises simulating data fetches with different delays
Create a variable to hold a timeout threshold in milliseconds
Use Promise.race to get the first promise that settles
Use Promise.allSettled to get the results of all promises
💡 Why This Matters
🌍 Real World
Fetching data from multiple APIs where you want either the fastest response or a full report of all responses including errors.
💼 Career
Understanding Promise.race and Promise.allSettled is essential for Node.js developers to handle multiple asynchronous operations efficiently and robustly.
Progress0 / 4 steps
1
Create three promises simulating data fetches
Create three promises named fetchUser, fetchPosts, and fetchComments. Each promise should resolve with a string: 'User data', 'Posts data', and 'Comments data' respectively. Use setTimeout to simulate delays of 3000ms, 2000ms, and 1000ms respectively.
Node.js
Hint
Use new Promise and setTimeout to simulate delayed responses.
2
Create a timeout threshold variable
Create a constant variable named timeout and set it to 2500 milliseconds. This will be used to compare delays.
Node.js
Hint
Use const timeout = 2500; to set the threshold.
3
Use Promise.race to get the first completed promise
Use Promise.race with an array containing fetchUser, fetchPosts, and fetchComments. Assign the result to a constant named fastestResponse.
Node.js
Hint
Use Promise.race with the three promises inside an array.
4
Use Promise.allSettled to get all results regardless of outcome
Use Promise.allSettled with an array containing fetchUser, fetchPosts, and fetchComments. Assign the result to a constant named allResults.
Node.js
Hint
Use Promise.allSettled with the three promises inside an array.
Practice
(1/5)
1. What does Promise.race do when given multiple promises?
easy
A. Cancels all promises except the first one.
B. Waits for all promises to finish and returns their results.
C. Returns only the results of promises that resolved successfully.
D. Returns the result or error of the first promise that finishes.
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 D
Quick Check:
Promise.race = first finished promise result [OK]
Hint: Remember race means first to finish wins [OK]
Common Mistakes:
Thinking it waits for all promises
Assuming it only returns successful results
Believing it cancels other promises
2. Which of the following is the correct syntax to use Promise.allSettled with an array of promises named tasks?
easy
A. Promise.allSettled(tasks).finally(() => console.log('done'));
B. Promise.allSettled(tasks).catch(error => console.log(error));
C. Promise.allSettled(tasks).then(results => console.log(results));
D. Promise.allSettled(tasks).resolve(results => console.log(results));
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 C
Quick Check:
Use .then() to get allSettled results [OK]
Hint: Use .then() to handle Promise.allSettled results [OK]
A. Promise.allSettled requires async/await syntax.
B. Promise.allSettled never rejects, so .catch will never run.
C. Promises array must contain only resolved promises.
D. You must use .then() before .catch() with allSettled.
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 B
Quick Check:
allSettled always resolves, .catch unused [OK]
Hint: allSettled never rejects, so .catch is useless here [OK]
Common Mistakes:
Thinking allSettled rejects on any promise failure
Believing async/await is required
Assuming .then() must come before .catch() always
5. You want to run three tasks: 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?
hard
A. Use Promise.race on all tasks, then if it rejects, run Promise.allSettled to get all errors.
B. Use Promise.allSettled first, then pick the first successful result from the array.
C. Use Promise.all on all tasks and catch errors to get all results.
D. Use Promise.race and ignore errors from rejected promises.
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 A
Quick Check:
race first success, allSettled for all errors [OK]
Hint: Race first success, fallback to allSettled for errors [OK]