Bird
Raised Fist0
Node.jsframework~20 mins

Promise.race and Promise.allSettled in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does Promise.race output?
Consider the following code snippet using Promise.race. What will be logged to the console?
Node.js
const p1 = new Promise(resolve => setTimeout(() => resolve('First'), 300));
const p2 = new Promise(resolve => setTimeout(() => resolve('Second'), 100));
Promise.race([p1, p2]).then(result => console.log(result));
APromise pending
B"First"
Cundefined
D"Second"
Attempts:
2 left
💡 Hint
Think about which promise resolves first based on the timeout durations.
state_output
intermediate
2:00remaining
What is the output of Promise.allSettled?
Given the following promises, what will Promise.allSettled return?
Node.js
const p1 = Promise.resolve('Success');
const p2 = Promise.reject('Error');
Promise.allSettled([p1, p2]).then(results => console.log(results));
APromise rejected with 'Error'
B[{status: 'fulfilled', value: 'Success'}, {status: 'fulfilled', value: 'Error'}]
C[{status: 'fulfilled', value: 'Success'}, {status: 'rejected', reason: 'Error'}]
D[{status: 'rejected', reason: 'Success'}, {status: 'rejected', reason: 'Error'}]
Attempts:
2 left
💡 Hint
Remember that allSettled waits for all promises and reports their status.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error with Promise.race?
Identify which code snippet will cause a syntax error when using Promise.race.
APromise.race(Promise.resolve(1), Promise.resolve(2)).then(console.log);
BPromise.race([Promise.resolve(1), Promise.reject(2)]).then(console.log);
CPromise.race([Promise.resolve(1), Promise.resolve(2)]).then(console.log);
DPromise.race([]).then(console.log);
Attempts:
2 left
💡 Hint
Check the expected argument type for Promise.race.
🔧 Debug
advanced
2:00remaining
Why does this Promise.allSettled code not log results?
This code does not log anything. What is the reason?
Node.js
const p1 = Promise.resolve('Done');
const p2 = Promise.reject('Fail');
Promise.allSettled([p1, p2]);
AMissing .then() to handle the results
BPromise.allSettled does not work with rejected promises
CPromises must be awaited with async/await
DThe array passed is empty
Attempts:
2 left
💡 Hint
Promises are lazy until you handle their result.
🧠 Conceptual
expert
2:00remaining
Which statement about Promise.race and Promise.allSettled is true?
Select the only true statement about Promise.race and Promise.allSettled:
APromise.allSettled resolves as soon as the first promise settles
BPromise.race resolves or rejects as soon as any promise settles
CPromise.race waits for all promises to settle before resolving
DPromise.allSettled rejects if any promise rejects
Attempts:
2 left
💡 Hint
Think about how each method handles multiple promises and their timing.

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

  1. Step 1: Understand Promise.race behavior

    Promise.race returns as soon as any promise settles (resolves or rejects).
  2. Step 2: Compare with other Promise methods

    Unlike Promise.all or allSettled, it does not wait for all promises.
  3. Final Answer:

    Returns the result or error of the first promise that finishes. -> Option D
  4. 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

  1. Step 1: Recall Promise.allSettled usage

    Promise.allSettled returns a promise that resolves with an array of results.
  2. Step 2: Check correct method chaining

    We use .then() to handle the resolved results, not .catch() or .resolve().
  3. Final Answer:

    Promise.allSettled(tasks).then(results => console.log(results)); -> Option C
  4. Quick Check:

    Use .then() to get allSettled results [OK]
Hint: Use .then() to handle Promise.allSettled results [OK]
Common Mistakes:
  • Using .catch() instead of .then() for results
  • Trying to use .resolve() method on promise
  • Assuming .finally() receives results
3. Consider the code:
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?
medium
A. 'Error'
B. 'A'
C. An array of results
D. Nothing, code throws syntax error

Solution

  1. Step 1: Identify which promise settles first

    p2 rejects after 50ms, p1 resolves after 100ms, so p2 finishes first.
  2. Step 2: Understand Promise.race behavior on rejection

    Promise.race rejects immediately with the first rejection, so .catch logs 'Error'.
  3. Final Answer:

    'Error' -> Option A
  4. Quick Check:

    First finished promise is rejection 'Error' [OK]
Hint: Check which promise settles first, resolve or reject [OK]
Common Mistakes:
  • Assuming resolve wins over reject
  • Expecting an array instead of single result
  • Thinking code throws syntax error
4. What is wrong with this code snippet?
const promises = [Promise.resolve(1), Promise.reject('fail')];
Promise.allSettled(promises).catch(console.error);
medium
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

  1. Step 1: Recall Promise.allSettled behavior

    It waits for all promises and never rejects, it always resolves with results.
  2. Step 2: Understand .catch usage here

    Since it never rejects, .catch will never be called, so error handling is ineffective.
  3. Final Answer:

    Promise.allSettled never rejects, so .catch will never run. -> Option B
  4. 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

  1. Step 1: Understand requirement for first success or all errors

    Promise.race gives first finished promise, but may reject if first is failure.
  2. Step 2: Combine Promise.race and Promise.allSettled

    If Promise.race rejects, then run Promise.allSettled to collect all errors from all tasks.
  3. Step 3: Evaluate other options

    Promise.allSettled alone waits for all, no early success; Promise.all fails fast; ignoring errors loses info.
  4. Final Answer:

    Use Promise.race on all tasks, then if it rejects, run Promise.allSettled to get all errors. -> Option A
  5. Quick Check:

    race first success, allSettled for all errors [OK]
Hint: Race first success, fallback to allSettled for errors [OK]
Common Mistakes:
  • Using allSettled only and waiting too long
  • Ignoring rejected promises in race
  • Using all which fails on first rejection