0
0
Javascriptprogramming~20 mins

Sequential vs parallel execution in Javascript - Practice Questions

Choose your learning style9 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.