Bird
Raised Fist0
Node.jsframework~10 mins

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

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
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.

Practice

(1/5)
1. What does Promise.all do in Node.js?
easy
A. Runs promises one after another in sequence
B. Runs only the first promise and ignores others
C. Runs multiple promises in parallel and waits for all to complete
D. Runs promises but returns results in random order

Solution

  1. Step 1: Understand Promise.all behavior

    Promise.all runs all promises at the same time (in parallel) and waits until all finish.
  2. Step 2: Check result order and completion

    It returns results in the order of the promises given, not random or sequentially one by one.
  3. Final Answer:

    Runs multiple promises in parallel and waits for all to complete -> Option C
  4. Quick Check:

    Promise.all = parallel run + wait all [OK]
Hint: Promise.all runs all promises together, not one by one [OK]
Common Mistakes:
  • Thinking Promise.all runs promises sequentially
  • Believing Promise.all returns results in random order
  • Assuming Promise.all ignores failed promises
2. Which of the following is the correct syntax to use Promise.all with an array of promises named promises?
easy
A. Promise.all(promises).then(results => console.log(results));
B. Promise.all.then(promises).catch(error => console.log(error));
C. Promise.all(promises).catch(results => console.log(results));
D. Promise.all(promises).finally(results => console.log(results));

Solution

  1. Step 1: Check Promise.all usage

    Promise.all is called as a function with an array of promises as argument.
  2. Step 2: Verify chaining with then()

    To get results, use .then() after Promise.all to handle resolved values.
  3. Final Answer:

    Promise.all(promises).then(results => console.log(results)); -> Option A
  4. Quick Check:

    Correct syntax = Promise.all(array).then() [OK]
Hint: Use Promise.all(array).then() to get results [OK]
Common Mistakes:
  • Using Promise.all.then(promises) instead of Promise.all(promises).then()
  • Using catch() to handle results instead of errors
  • Using finally() to get results instead of then()
3. What will be logged by this code?
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.resolve(3);

Promise.all([p1, p2, p3]).then(results => console.log(results));
medium
A. Error: Promise rejected
B. [1, 2, 3]
C. [undefined, undefined, undefined]
D. [3, 2, 1]

Solution

  1. Step 1: Understand resolved promises

    p1, p2, p3 are promises resolved immediately with values 1, 2, and 3.
  2. Step 2: Promise.all returns array of results in input order

    Promise.all waits for all to resolve and returns results in the same order as input array.
  3. Final Answer:

    [1, 2, 3] -> Option B
  4. Quick Check:

    Promise.all results order = input order [OK]
Hint: Promise.all returns results in same order as input promises [OK]
Common Mistakes:
  • Assuming results order depends on resolution speed
  • Expecting reversed or random order
  • Thinking Promise.all returns undefined values
4. Identify the error in this code snippet:
const p1 = Promise.resolve('A');
const p2 = Promise.reject('Error');

Promise.all([p1, p2])
  .then(results => console.log('Results:', results))
  .catch(error => console.log('Caught:', error));
medium
A. The catch block will run with 'Error' because p2 rejects
B. Promise.all will never reject even if one promise fails
C. The then block will run with partial results
D. Syntax error: Promise.reject cannot be used inside Promise.all

Solution

  1. Step 1: Check behavior of Promise.all with rejected promises

    If any promise rejects, Promise.all immediately rejects with that error.
  2. Step 2: Analyze catch and then blocks

    Since p2 rejects, the catch block runs with the error message 'Error'. The then block does not run.
  3. Final Answer:

    The catch block will run with 'Error' because p2 rejects -> Option A
  4. Quick Check:

    Promise.all rejects if any promise rejects [OK]
Hint: If one promise rejects, Promise.all rejects immediately [OK]
Common Mistakes:
  • Thinking Promise.all ignores rejected promises
  • Expecting then block to run with partial results
  • Believing Promise.reject causes syntax error here
5. You want to fetch data from three APIs in parallel and process all results only if all succeed. Which code correctly uses Promise.all to achieve this?
const fetch1 = () => fetch('https://api1.example.com/data').then(res => res.json());
const fetch2 = () => fetch('https://api2.example.com/data').then(res => res.json());
const fetch3 = () => fetch('https://api3.example.com/data').then(res => res.json());

// Which code snippet correctly waits for all fetches and handles errors?
hard
A. Promise.all([fetch1(), fetch2(), fetch3()]) .then(results => console.error('All data:', results)) .catch(error => console.log('Fetch failed:', error));
B. Promise.all([fetch1, fetch2, fetch3]) .then(results => console.log('All data:', results)) .catch(error => console.error('Fetch failed:', error));
C. Promise.all(fetch1, fetch2, fetch3) .then(results => console.log('All data:', results)) .catch(error => console.error('Fetch failed:', error));
D. Promise.all([fetch1(), fetch2(), fetch3()]) .then(results => console.log('All data:', results)) .catch(error => console.error('Fetch failed:', error));

Solution

  1. Step 1: Check how to call fetch functions

    fetch1, fetch2, fetch3 are functions returning promises, so call them with () to get promises.
  2. Step 2: Verify Promise.all usage and error handling

    Pass an array of promises to Promise.all, then use .then() to handle results and .catch() for errors.
  3. Final Answer:

    Promise.all([fetch1(), fetch2(), fetch3()]) .then(results => console.log('All data:', results)) .catch(error => console.error('Fetch failed:', error)); -> Option D
  4. Quick Check:

    Call functions to get promises, pass array to Promise.all [OK]
Hint: Call functions to get promises before Promise.all [OK]
Common Mistakes:
  • Passing functions instead of calling them
  • Passing multiple arguments instead of an array
  • Swapping console.log and console.error in then/catch