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.all for Parallel Execution in Node.js
📖 Scenario: You are building a Node.js script that fetches data from multiple URLs. To make the process faster, you want to run all fetches at the same time instead of waiting for each one to finish before starting the next.
🎯 Goal: Build a Node.js script that uses Promise.all to fetch data from multiple URLs in parallel and handle the results together.
📋 What You'll Learn
Create an array called urls with three exact URL strings.
Create a function called fetchData that returns a promise resolving with a string.
Use Promise.all with urls.map(fetchData) to run all fetches in parallel.
Add a final .then block to handle the array of results.
💡 Why This Matters
🌍 Real World
Fetching multiple data sources at the same time speeds up web applications and scripts, improving user experience and efficiency.
💼 Career
Understanding Promise.all is essential for Node.js developers to handle multiple asynchronous operations efficiently in real projects.
Progress0 / 4 steps
1
Create the URLs array
Create an array called urls with these exact strings: 'https://api.example.com/data1', 'https://api.example.com/data2', and 'https://api.example.com/data3'.
Node.js
Hint
Use square brackets [] to create an array and separate the URLs with commas.
2
Create the fetchData function
Create a function called fetchData that takes a parameter url and returns a promise that resolves with the string `Data from ${url}` after 100 milliseconds using setTimeout.
Node.js
Hint
Use new Promise and setTimeout to simulate an async fetch that resolves after 100 milliseconds.
3
Use Promise.all to fetch all URLs in parallel
Create a variable called allData and assign it the result of Promise.all called with urls.map(fetchData) to run all fetches in parallel.
Node.js
Hint
Use urls.map(fetchData) inside Promise.all to run all fetches at the same time.
4
Add a then block to handle all results
Add a .then block to allData that takes a parameter results and inside the block, create a variable firstResult assigned to results[0].
Node.js
Hint
Use allData.then(results => { ... }) and inside the block assign const firstResult = results[0].
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
Step 1: Understand Promise.all behavior
Promise.all runs all promises at the same time (in parallel) and waits until all finish.
Step 2: Check result order and completion
It returns results in the order of the promises given, not random or sequentially one by one.
Final Answer:
Runs multiple promises in parallel and waits for all to complete -> Option C
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
Step 1: Check Promise.all usage
Promise.all is called as a function with an array of promises as argument.
Step 2: Verify chaining with then()
To get results, use .then() after Promise.all to handle resolved values.
Final Answer:
Promise.all(promises).then(results => console.log(results)); -> Option A
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()
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
Step 1: Check behavior of Promise.all with rejected promises
If any promise rejects, Promise.all immediately rejects with that error.
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.
Final Answer:
The catch block will run with 'Error' because p2 rejects -> Option A
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?