Sequential execution means running code one step at a time, in order. Each step waits for the previous one to finish before starting.
Click to reveal answer
beginner
What does parallel execution mean?
Parallel execution means running multiple tasks at the same time, so they can finish faster by working together.
Click to reveal answer
intermediate
How does JavaScript handle parallel execution since it has a single thread?
JavaScript uses asynchronous features like promises and async/await to start tasks that run in the background, allowing other code to run without waiting.
Click to reveal answer
intermediate
What is the role of Promise.all() in parallel execution?
Promise.all() lets you run multiple promises at the same time and wait for all of them to finish before continuing.
Click to reveal answer
intermediate
Give an example of sequential vs parallel execution using async functions.
Sequential: await task1(); await task2(); runs task2 after task1 finishes. Parallel: await Promise.all([task1(), task2()]); runs both tasks at the same time.
Click to reveal answer
Which of these best describes sequential execution?
ATasks run randomly
BTasks run all at the same time
CTasks run one after another, waiting for each to finish
DTasks never finish
✗ Incorrect
Sequential execution means tasks run one by one, each waiting for the previous to finish.
What JavaScript feature helps run tasks in parallel?
AConsole.log
BFor loops
CVariables
DPromises and async/await
✗ Incorrect
Promises and async/await allow JavaScript to handle tasks asynchronously, enabling parallel execution.
What does Promise.all() do?
ARuns multiple promises in parallel and waits for all to finish
BRuns promises one by one
CCancels all promises
DCreates a new promise that never resolves
✗ Incorrect
Promise.all() runs promises together and waits until all complete.
If you write await task1(); await task2();, how do tasks run?
AIn parallel, both start together
BSequentially, task2 waits for task1
COnly task2 runs
DBoth tasks are skipped
✗ Incorrect
Using await one after another runs tasks sequentially.
Why is parallel execution useful?
AIt can make programs faster by doing many things at once
BIt makes code harder to read
CIt always uses more memory
DIt stops programs from running
✗ Incorrect
Parallel execution helps programs finish tasks faster by running them simultaneously.
Explain the difference between sequential and parallel execution in JavaScript.
Think about waiting for tasks to finish or running them together.
You got /3 concepts.
How does Promise.all() help with parallel execution?
It collects many promises and waits for all.
You got /3 concepts.
Practice
(1/5)
1.
What is the main difference between sequential and parallel execution in JavaScript?
easy
A. Sequential uses promises; parallel uses only callbacks.
B. Sequential runs tasks one after another; parallel runs tasks at the same time.
C. Sequential runs tasks at the same time; parallel runs tasks one after another.
D. Sequential is faster than parallel always.
Solution
Step 1: Understand sequential execution
Sequential execution means tasks run one by one, waiting for each to finish before starting the next.
Step 2: Understand parallel execution
Parallel execution means tasks run at the same time, so they can finish faster by overlapping.
Final Answer:
Sequential runs tasks one after another; parallel runs tasks at the same time. -> Option B
Quick Check:
Sequential = one by one, Parallel = at the same time [OK]
Hint: Remember: sequential = one by one, parallel = together [OK]
Common Mistakes:
Confusing which runs tasks simultaneously
Thinking sequential is always faster
Mixing up async/await with execution type
2.
Which of the following is the correct syntax to run two promises in parallel using Promise.all?
const p1 = fetch(url1);
const p2 = fetch(url2);
// What is correct?
easy
A. const results = await Promise.race([p1, p2]);
B. const results = await Promise.parallel([p1, p2]);
C. const results = await Promise.sequence([p1, p2]);
D. const results = await Promise.all([p1, p2]);
Solution
Step 1: Identify correct Promise method for parallel
Promise.all runs multiple promises in parallel and waits for all to finish.
Step 2: Check syntax correctness
Using await Promise.all([p1, p2]) correctly waits for both promises in parallel.
Final Answer:
const results = await Promise.all([p1, p2]); -> Option D
Quick Check:
Promise.all = parallel wait [OK]
Hint: Use Promise.all for parallel waits, not race or sequence [OK]
Common Mistakes:
Using Promise.race which waits only for first promise
Using non-existent Promise.sequence or Promise.parallel
Not wrapping promises in an array
3.
What will be the output of this code?
async function test() {
console.log('Start');
await new Promise(r => setTimeout(r, 1000));
console.log('Middle');
await new Promise(r => setTimeout(r, 1000));
console.log('End');
}
test();
medium
A. Start immediately, then Middle after 1s, then End after 2s
B. Start, Middle, and End all print immediately
C. Start, End, then Middle after 2 seconds
D. Only Start prints, then program stops
Solution
Step 1: Analyze first await delay
After printing 'Start', the code waits 1 second before printing 'Middle'.
Step 2: Analyze second await delay
After 'Middle', it waits another 1 second before printing 'End'.
Final Answer:
Start immediately, then Middle after 1s, then End after 2s -> Option A
Quick Check:
await pauses sequentially, so output is delayed stepwise [OK]
Hint: await pauses code, so logs appear one after another [OK]
Common Mistakes:
Thinking all logs print immediately
Confusing setTimeout with immediate execution
Ignoring await pauses
4.
Find the error in this code that tries to run two async tasks in parallel: