0
0
Node.jsframework~10 mins

Promise.race and Promise.allSettled in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Promise.race and Promise.allSettled
Start multiple promises
First promise settles
Resolve or reject with first settled promise
Wait for all promises to settle
Return array of all settled results
Promise.race returns the result of the first promise that settles (resolve or reject). Promise.allSettled waits for all promises to finish and returns their results regardless of success or failure.
Execution Sample
Node.js
const p1 = new Promise(r => setTimeout(() => r('A'), 300));
const p2 = new Promise((_, rej) => setTimeout(() => rej('B'), 200));

Promise.race([p1, p2]).then(console.log).catch(console.error);

Promise.allSettled([p1, p2]).then(console.log);
Runs two promises: one resolves after 300ms, one rejects after 200ms. Shows how Promise.race returns first settled, Promise.allSettled waits for both.
Execution Table
StepPromiseActionTime (ms)ResultPromise.race OutcomePromise.allSettled Outcome
1p1Started0PendingPendingPending
2p2Started0PendingPendingPending
3p2Rejects200Rejected with 'B'Rejected with 'B'Pending
4p1Resolves300Resolved with 'A'Rejected with 'B'Pending
5All settledAll promises settled300p1: fulfilled 'A', p2: rejected 'B'Rejected with 'B'[{status:'fulfilled', value:'A'}, {status:'rejected', reason:'B'}]
💡 Promise.race settles at 200ms with p2 rejection; Promise.allSettled waits until 300ms when both promises have settled.
Variable Tracker
VariableStartAfter 1After 2After 3Final
p1PendingPendingPendingPendingResolved 'A'
p2PendingPendingRejected 'B'Rejected 'B'Rejected 'B'
Promise.race OutcomePendingPendingRejected 'B'Rejected 'B'Rejected 'B'
Promise.allSettled OutcomePendingPendingPendingPending[{status:'fulfilled', value:'A'}, {status:'rejected', reason:'B'}]
Key Moments - 3 Insights
Why does Promise.race settle with the rejected promise even though another promise resolves later?
Because Promise.race settles as soon as the first promise settles, whether it resolves or rejects. See execution_table row 3 where p2 rejects first, so race settles immediately.
Why does Promise.allSettled wait for both promises even if one rejects early?
Promise.allSettled waits for all promises to settle regardless of resolve or reject. It collects all results. See execution_table rows 3 and 4 where it waits until both p1 and p2 finish.
What is the difference in the output format between Promise.race and Promise.allSettled?
Promise.race returns the value or reason of the first settled promise directly. Promise.allSettled returns an array of objects describing each promise's status and value or reason. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3: what is the Promise.race outcome?
ARejected with 'B'
BResolved with 'A'
CPending
DResolved with 'B'
💡 Hint
Check the 'Promise.race Outcome' column at step 3 in the execution_table.
At which step does Promise.allSettled return its final array of results?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look at the 'Promise.allSettled Outcome' column and find when it shows the full array.
If p2 resolved instead of rejected at 200ms, what would Promise.race return at step 3?
ARejected with 'B'
BResolved with 'A'
CResolved with p2's value
DPending
💡 Hint
Promise.race returns the first promise that settles, so if p2 resolves first, it returns p2's value.
Concept Snapshot
Promise.race(promises): returns the first promise that settles (resolve or reject).
Promise.allSettled(promises): waits for all promises to settle, returns array of {status, value/reason}.
Use race to get fastest result; use allSettled to get all results regardless of success.
Both return promises themselves.
Race settles early; allSettled waits for all.
Full Transcript
This visual trace shows how Promise.race and Promise.allSettled behave with two promises: one resolving after 300ms and one rejecting after 200ms. Promise.race settles as soon as the first promise settles, which is the rejection at 200ms, so it returns that rejection immediately. Promise.allSettled waits for both promises to finish, then returns an array describing each promise's outcome. Variables track promise states and outcomes step-by-step. Key moments clarify why race settles early and allSettled waits for all. The quiz tests understanding of timing and results based on the execution table.