0
0
Node.jsframework~10 mins

Promise.all for parallel execution in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Promise.all for parallel execution
Start multiple promises
Promises run in parallel
Wait for all promises to resolve
If any promise rejects, stop and reject
All promises resolved
Return array of results
This flow shows how Promise.all starts multiple promises at once, waits for all to finish, and then returns all results together or rejects if any fail.
Execution Sample
Node.js
const p1 = new Promise(resolve => setTimeout(() => resolve('A'), 10));
const p2 = new Promise(resolve => setTimeout(() => resolve('B'), 20));
Promise.all([p1, p2]).then(results => console.log(results));
Runs two promises in parallel and logs their results as an array when both finish.
Execution Table
StepActionPromise StatesResult ArrayNotes
1Start p1 and p2p1: pending, p2: pending[]Both promises begin running in parallel
2p1 resolvesp1: fulfilled('A'), p2: pending[]p1 finished, waiting for p2
3p2 resolvesp1: fulfilled('A'), p2: fulfilled('B')['A', 'B']All promises fulfilled, results collected
4Promise.all resolvesAll fulfilled['A', 'B']Then callback runs with results array
💡 All promises fulfilled, Promise.all returns array of results
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
p1pendingpendingfulfilled('A')fulfilled('A')fulfilled('A')
p2pendingpendingpendingfulfilled('B')fulfilled('B')
resultsundefinedundefinedundefined['A', 'B']['A', 'B']
Key Moments - 2 Insights
Why does Promise.all wait for all promises instead of returning after the first one resolves?
Promise.all collects results from all promises. It only resolves when every promise is fulfilled, as shown in execution_table rows 2 and 3 where it waits for p2 after p1 resolves.
What happens if one promise rejects?
Promise.all immediately rejects with that error and stops waiting for others. This is why it’s important to handle errors. This is implied in the concept_flow where rejection stops the process.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of p2 at Step 2?
Afulfilled('B')
Bpending
Crejected
Dundefined
💡 Hint
Check the 'Promise States' column at Step 2 in the execution_table
At which step does Promise.all resolve and provide the results array?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Result Array' and 'Notes' columns in the execution_table
If p1 rejected instead of resolving, what would happen to Promise.all?
AIt would wait for p2 and then resolve
BIt would ignore p1 and resolve with p2's result
CIt would immediately reject and stop waiting
DIt would resolve with an empty array
💡 Hint
Refer to the concept_flow where rejection stops Promise.all immediately
Concept Snapshot
Promise.all takes an array of promises and runs them all at once.
It waits until all promises resolve, then returns an array of their results.
If any promise rejects, Promise.all rejects immediately.
Use .then() to get results or .catch() to handle errors.
Great for running tasks in parallel and waiting for all to finish.
Full Transcript
Promise.all runs multiple promises at the same time. It starts all promises together and waits for every one of them to finish. If all promises succeed, Promise.all returns an array with all their results in order. If any promise fails, Promise.all stops and returns the error immediately. This helps run tasks in parallel and get all results together. The execution table shows each step: starting promises, each promise resolving, and finally Promise.all resolving with the results array.