Bird
Raised Fist0
Javascriptprogramming~20 mins

Sequential vs parallel execution in Javascript - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Sequential and Parallel Execution
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of sequential async calls
What will be the output of this JavaScript code that runs two async functions sequentially?
Javascript
async function task(name, delay) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(name);
      resolve(name);
    }, delay);
  });
}

async function run() {
  await task('First', 100);
  await task('Second', 50);
  console.log('Done');
}

run();
AFirst\nDone\nSecond
BSecond\nFirst\nDone
CDone\nFirst\nSecond
DFirst\nSecond\nDone
Attempts:
2 left
💡 Hint
Remember that await pauses the function until the promise resolves.
❓ Predict Output
intermediate
2:00remaining
Output of parallel async calls with Promise.all
What will be the output of this JavaScript code that runs two async functions in parallel?
Javascript
async function task(name, delay) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(name);
      resolve(name);
    }, delay);
  });
}

async function run() {
  await Promise.all([task('First', 100), task('Second', 50)]);
  console.log('Done');
}

run();
ASecond\nFirst\nDone
BFirst\nSecond\nDone
CDone\nFirst\nSecond
DFirst\nDone\nSecond
Attempts:
2 left
💡 Hint
Promise.all runs promises in parallel and waits for all to finish.
❓ Predict Output
advanced
2:00remaining
Output of mixed sequential and parallel async calls
What will be the output of this code that mixes sequential and parallel async calls?
Javascript
async function task(name, delay) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(name);
      resolve(name);
    }, delay);
  });
}

async function run() {
  await task('First', 100);
  await Promise.all([task('Second', 50), task('Third', 70)]);
  console.log('Done');
}

run();
ASecond\nThird\nFirst\nDone
BFirst\nSecond\nThird\nDone
CFirst\nDone\nSecond\nThird
DFirst\nThird\nSecond\nDone
Attempts:
2 left
💡 Hint
The first task runs alone, then the next two run together.
❓ Predict Output
advanced
2:00remaining
Output order with Promise.race
What will be the output of this code using Promise.race with two async tasks?
Javascript
async function task(name, delay) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(name);
      resolve(name);
    }, delay);
  });
}

async function run() {
  await Promise.race([task('First', 100), task('Second', 50)]);
  console.log('Done');
}

run();
ASecond\nDone\nFirst
BFirst\nDone
CDone\nFirst\nSecond
DFirst\nSecond\nDone
Attempts:
2 left
💡 Hint
Promise.race resolves as soon as the first promise resolves.
🧠 Conceptual
expert
2:00remaining
Understanding event loop and parallelism in JavaScript
Which statement best describes how JavaScript handles parallel execution and the event loop?
AJavaScript runs multiple threads in parallel to execute code simultaneously.
BJavaScript executes all code synchronously and blocks until each task finishes.
CJavaScript uses a single thread and runs asynchronous tasks via the event loop to simulate parallelism.
DJavaScript creates a new thread for each async function automatically.
Attempts:
2 left
💡 Hint
Think about how JavaScript handles async tasks without real threads.

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

  1. Step 1: Understand sequential execution

    Sequential execution means tasks run one by one, waiting for each to finish before starting the next.
  2. Step 2: Understand parallel execution

    Parallel execution means tasks run at the same time, so they can finish faster by overlapping.
  3. Final Answer:

    Sequential runs tasks one after another; parallel runs tasks at the same time. -> Option B
  4. 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

  1. Step 1: Identify correct Promise method for parallel

    Promise.all runs multiple promises in parallel and waits for all to finish.
  2. Step 2: Check syntax correctness

    Using await Promise.all([p1, p2]) correctly waits for both promises in parallel.
  3. Final Answer:

    const results = await Promise.all([p1, p2]); -> Option D
  4. 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

  1. Step 1: Analyze first await delay

    After printing 'Start', the code waits 1 second before printing 'Middle'.
  2. Step 2: Analyze second await delay

    After 'Middle', it waits another 1 second before printing 'End'.
  3. Final Answer:

    Start immediately, then Middle after 1s, then End after 2s -> Option A
  4. 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:

async function runTasks() {
  const result1 = await task1();
  const result2 = await task2();
  return [result1, result2];
}
medium
A. Cannot return array from async function.
B. Missing async keyword on task1 and task2.
C. Tasks run sequentially, not in parallel.
D. await cannot be used inside async functions.

Solution

  1. Step 1: Check how tasks are awaited

    Each await waits for task1 to finish before starting task2, so tasks run one after another.
  2. Step 2: Identify parallel execution fix

    To run in parallel, start both tasks without await, then await both together using Promise.all.
  3. Final Answer:

    Tasks run sequentially, not in parallel. -> Option C
  4. Quick Check:

    Separate awaits cause sequential execution [OK]
Hint: Awaiting one by one is sequential, start all then await all for parallel [OK]
Common Mistakes:
  • Assuming await runs tasks in parallel
  • Thinking async keyword fixes parallelism alone
  • Believing async functions can't return arrays
5.

You want to fetch data from three URLs and process results only after all are done. Which code correctly runs fetches in parallel and waits for all?

const urls = ['url1', 'url2', 'url3'];
async function fetchAll() {
  // Fill this
}
hard
A. const results = await Promise.all(urls.map(url => fetch(url)));
B. const results = []; for (const url of urls) { results.push(await fetch(url)); }
C. const results = urls.map(url => await fetch(url));
D. const results = await urls.forEach(url => fetch(url));

Solution

  1. Step 1: Understand parallel fetch with Promise.all

    Using Promise.all with map starts all fetches at once and waits for all to finish.
  2. Step 2: Check other options for errors

    const results = []; for (const url of urls) { results.push(await fetch(url)); } fetches sequentially due to await inside loop; const results = urls.map(url => await fetch(url)); uses await incorrectly inside map; const results = await urls.forEach(url => fetch(url)); misuses forEach which returns undefined.
  3. Final Answer:

    const results = await Promise.all(urls.map(url => fetch(url))); -> Option A
  4. Quick Check:

    Promise.all + map = parallel fetch [OK]
Hint: Use Promise.all with map for parallel async calls [OK]
Common Mistakes:
  • Await inside for loop causing sequential fetch
  • Using await inside map callback directly
  • Using forEach which does not return promises
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard