0
0
Node.jsframework~20 mins

Sequential vs parallel async execution in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Execution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding async function execution order
Consider two async functions called one after another without awaiting the first. What is the behavior of their execution?
Node.js
async function first() {
  console.log('First start');
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log('First end');
}

async function second() {
  console.log('Second start');
  await new Promise(resolve => setTimeout(resolve, 50));
  console.log('Second end');
}

first();
second();
ALogs 'First start', then 'Second start', then 'Second end', then 'First end' in that order.
BLogs 'First start', 'First end', 'Second start', 'Second end' in that order.
CLogs 'Second start', 'Second end', 'First start', 'First end' in that order.
DLogs 'First start', 'Second start', 'First end', 'Second end' in that order.
Attempts:
2 left
💡 Hint
Think about how async functions start immediately but pause at await.
component_behavior
intermediate
2:00remaining
Effect of awaiting promises sequentially vs in parallel
What is the total approximate time taken by the following two code snippets, assuming each asyncTask takes 100ms to complete?
Node.js
async function asyncTask() {
  return new Promise(resolve => setTimeout(resolve, 100));
}

// Snippet 1
async function sequential() {
  await asyncTask();
  await asyncTask();
}

// Snippet 2
async function parallel() {
  await Promise.all([asyncTask(), asyncTask()]);
}
ABoth snippets take about 200ms to complete.
BSequential takes about 100ms; parallel takes about 200ms.
CBoth snippets take about 100ms to complete.
DSequential takes about 200ms; parallel takes about 100ms.
Attempts:
2 left
💡 Hint
Consider if tasks run one after the other or at the same time.
🔧 Debug
advanced
2:00remaining
Identify the error in parallel async execution
What error will this code produce when run in Node.js 20+?
Node.js
async function fetchData(id) {
  return new Promise(resolve => setTimeout(() => resolve(id), 100));
}

async function run() {
  const results = await Promise.all(fetchData(1), fetchData(2));
  console.log(results);
}

run();
ASyntaxError due to missing brackets in Promise.all.
B[1, 2] logged after 100ms delay.
CTypeError: Promise.all accepts an iterable but received multiple arguments.
DReferenceError: fetchData is not defined.
Attempts:
2 left
💡 Hint
Check how Promise.all expects its arguments.
state_output
advanced
2:00remaining
State changes in sequential vs parallel async updates
Given this code, what will be the final value of count after running sequential() and parallel() respectively?
Node.js
let count = 0;

function delayAdd(value) {
  return new Promise(resolve => setTimeout(() => {
    count += value;
    resolve(count);
  }, 100));
}

async function sequential() {
  await delayAdd(1);
  await delayAdd(2);
}

async function parallel() {
  await Promise.all([delayAdd(1), delayAdd(2)]);
}
AAfter sequential: count = 3; after parallel: count = 2
BAfter sequential: count = 3; after parallel: count = 3
CAfter sequential: count = 3; after parallel: count = 1
DAfter sequential: count = 1; after parallel: count = 3
Attempts:
2 left
💡 Hint
Consider how count is updated in both cases.
📝 Syntax
expert
2:00remaining
Correct syntax for parallel async execution with error handling
Which option correctly runs two async tasks in parallel and catches errors properly?
Node.js
async function task1() {
  throw new Error('fail1');
}

async function task2() {
  return 'success2';
}

async function run() {
  // Fill in the blank
}
A
try {
  const results = await Promise.all([task1(), task2()]);
  console.log(results);
} catch (e) {
  console.error('Error:', e.message);
}
B
const results = await Promise.allSettled([task1(), task2()]);
console.log(results);
C
try {
  const results = await Promise.all(task1(), task2());
  console.log(results);
} catch (e) {
  console.error('Error:', e.message);
}
D
const results = await Promise.all([task1(), task2()]).catch(e => console.error(e));
console.log(results);
Attempts:
2 left
💡 Hint
Check how Promise.all and try/catch work together.