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
Sequential vs Parallel Async Execution in Node.js
📖 Scenario: You are building a simple Node.js script that fetches data from two different sources asynchronously. You want to learn how to run these fetches one after another (sequentially) and how to run them at the same time (in parallel) to see the difference in execution.
🎯 Goal: Build a Node.js script that first fetches two pieces of data sequentially using async/await, then modify it to fetch them in parallel using Promise.all. You will see how the total time changes depending on the approach.
📋 What You'll Learn
Create two async functions that simulate fetching data with a delay
Create a variable to hold the delay time in milliseconds
Write code to fetch data sequentially using async/await
Write code to fetch data in parallel using Promise.all
💡 Why This Matters
🌍 Real World
Many Node.js applications fetch data from multiple sources like APIs or databases. Knowing how to run these fetches sequentially or in parallel helps optimize performance and user experience.
💼 Career
Understanding async patterns like sequential and parallel execution is essential for backend and full-stack developers working with Node.js to write efficient and responsive applications.
Progress0 / 4 steps
1
Create async fetch functions
Create two async functions called fetchData1 and fetchData2. Each should return a Promise that resolves with the strings 'Data 1' and 'Data 2' respectively after a delay of 1000 milliseconds using setTimeout.
Node.js
Hint
Use new Promise and setTimeout inside each async function to simulate delay.
2
Add delay variable
Create a constant variable called delay and set it to 1000 to represent the delay time in milliseconds.
Node.js
Hint
Use const delay = 1000; to store the delay time.
3
Fetch data sequentially
Write an async function called fetchSequential. Inside it, use await to call fetchData1() and store the result in result1. Then awaitfetchData2() and store the result in result2. Return an array with result1 and result2.
Node.js
Hint
Use await to wait for each fetch before starting the next.
4
Fetch data in parallel
Write an async function called fetchParallel. Inside it, use Promise.all with an array of fetchData1() and fetchData2() calls. Store the result in results and return it.
Node.js
Hint
Use Promise.all to run both fetches at the same time and wait for both to finish.
Practice
(1/5)
1. What is the main difference between sequential and parallel async execution in Node.js?
easy
A. Sequential async is faster than parallel async in all cases.
B. Sequential async waits for each task to finish before starting the next, while parallel async runs tasks at the same time.
C. Sequential async uses callbacks, while parallel async uses promises.
D. Sequential async runs all tasks at once, while parallel async runs them one by one.
Solution
Step 1: Understand sequential async execution
Sequential async means tasks run one after another, waiting for each to complete before starting the next.
Step 2: Understand parallel async execution
Parallel async means tasks run at the same time, without waiting for others to finish first.
Final Answer:
Sequential async waits for each task to finish before starting the next, while parallel async runs tasks at the same time. -> Option B
Quick Check:
Sequential vs parallel async = D [OK]
Hint: Sequential waits, parallel runs together [OK]
Common Mistakes:
Confusing which runs tasks one by one
Thinking parallel always uses callbacks
Assuming sequential is always faster
2. Which of the following is the correct syntax to run two async functions task1() and task2() in parallel using Promise.all?
easy
A. await Promise.all([task1(), task2()]);
B. Promise.all(task1(), task2());
C. await task1(); await task2();
D. task1().then(task2());
Solution
Step 1: Recall Promise.all syntax
Promise.all takes an array of promises and waits for all to complete in parallel.
Step 2: Check correct usage
The correct syntax is await Promise.all([task1(), task2()]) to run both tasks in parallel and wait for both.
Final Answer:
await Promise.all([task1(), task2()]); -> Option A
Quick Check:
Promise.all needs array of promises [OK]
Hint: Use Promise.all with array for parallel [OK]
A. Promise.all requires an array of promises, but here arguments are passed separately.
B. Await cannot be used with Promise.all.
C. task1 and task2 must be awaited separately before Promise.all.
D. console.log cannot print arrays.
Solution
Step 1: Check Promise.all argument
Promise.all expects a single array of promises, but here two arguments are passed separately.
Step 2: Correct usage
It should be Promise.all([task1(), task2()]) with square brackets to group promises.
Final Answer:
Promise.all requires an array of promises, but here arguments are passed separately. -> Option A
Quick Check:
Promise.all needs array argument [OK]
Hint: Promise.all needs array, not separate args [OK]
Common Mistakes:
Passing promises as separate arguments
Thinking await can't be used with Promise.all
Misunderstanding console.log capabilities
5. You have three independent async tasks: taskA(), taskB(), and taskC(). You want to run taskA and taskB in parallel, then run taskC only after both finish. Which code correctly implements this?
hard
A. const c = await taskC();
const [a, b] = await Promise.all([taskA(), taskB()]);
B. await taskA(); await taskB(); await taskC();
C. const [a, b] = await Promise.all([taskA(), taskB()]);
const c = await taskC();
D. Promise.all([taskA(), taskB(), taskC()]);
Solution
Step 1: Run taskA and taskB in parallel
Using await Promise.all([taskA(), taskB()]) runs both tasks at the same time and waits for both to finish.
Step 2: Run taskC after both finish
After awaiting both, await taskC() runs taskC sequentially, ensuring it starts only after taskA and taskB complete.
Final Answer:
const [a, b] = await Promise.all([taskA(), taskB()]);
const c = await taskC(); -> Option C
Quick Check:
Parallel first, then sequential = A [OK]
Hint: Use Promise.all for parallel, then await next task [OK]