Bird
Raised Fist0
Node.jsframework~20 mins

Event loop mental model in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Event Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output order of this Node.js code?
Consider this Node.js code snippet. What will be the order of the printed lines in the console?
Node.js
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
console.log('End');
A"Start", "Promise", "End", "Timeout"
B"Promise", "Start", "End", "Timeout"
C"Start", "Timeout", "Promise", "End"
D"Start", "End", "Promise", "Timeout"
Attempts:
2 left
💡 Hint
Remember that promises run before timers even if the timer delay is zero.
state_output
intermediate
2:00remaining
What is the final value of counter after this code runs?
Given this Node.js code, what is the final value of the variable counter after all asynchronous tasks complete?
Node.js
let counter = 0;
setTimeout(() => { counter += 1; }, 0);
Promise.resolve().then(() => { counter += 2; });
counter += 3;
A6
B3
C5
D2
Attempts:
2 left
💡 Hint
Synchronous code runs first, then microtasks, then timers.
📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError in this async code?
Identify which code snippet will cause a SyntaxError when run in Node.js 20+.
Aasync function test() { await 5; }
Basync function test() { return await 5; }
Casync function test() { await; }
Dconst test = async () => { await Promise.resolve(); };
Attempts:
2 left
💡 Hint
The await keyword must be followed by an expression.
🔧 Debug
advanced
2:00remaining
Why does this code never print 'Done'?
Examine the code below. Why does it never print 'Done' to the console?
Node.js
setImmediate(() => {
  while(true) {}
  console.log('Done');
});
A'setImmediate' callbacks never run in Node.js.
BThe infinite loop blocks the event loop, so 'Done' is never reached.
CThe console.log is outside the callback function.
DThe code throws a ReferenceError before running.
Attempts:
2 left
💡 Hint
Think about what happens when the event loop is blocked.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the Node.js event loop phases?
Select the option that correctly orders the main phases of the Node.js event loop.
ATimers → Pending Callbacks → Poll → Check → Close Callbacks
BPoll → Timers → Check → Pending Callbacks → Close Callbacks
CTimers → Poll → Pending Callbacks → Check → Close Callbacks
DCheck → Timers → Poll → Pending Callbacks → Close Callbacks
Attempts:
2 left
💡 Hint
Remember the event loop starts with timers phase.

Practice

(1/5)
1. Which part of the Node.js event loop runs Promise callbacks before timers?
easy
A. I/O callbacks phase
B. Timers phase
C. Microtasks queue
D. Check phase

Solution

  1. Step 1: Understand event loop phases

    The event loop has phases: timers, I/O callbacks, idle, poll, check, close callbacks, and microtasks run between phases.
  2. Step 2: Identify when Promise callbacks run

    Promise callbacks are microtasks and run immediately after the current operation, before timers and I/O callbacks.
  3. Final Answer:

    Microtasks queue -> Option C
  4. Quick Check:

    Promises run in microtasks before timers [OK]
Hint: Remember: promises run before timers in microtasks [OK]
Common Mistakes:
  • Thinking timers run before promises
  • Confusing I/O callbacks with microtasks
  • Assuming check phase runs before microtasks
2. Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?
easy
A. setTimeout(myFunc, 0);
B. setInterval(myFunc, 0);
C. process.nextTick(myFunc);
D. setImmediate(myFunc, 0);

Solution

  1. Step 1: Identify function to run after delay

    setTimeout schedules a function after a specified delay in milliseconds.
  2. Step 2: Check syntax for zero delay

    Using setTimeout(myFunc, 0) runs myFunc after the current call stack is empty, effectively scheduling it soon.
  3. Final Answer:

    setTimeout(myFunc, 0); -> Option A
  4. Quick Check:

    setTimeout with 0 delay schedules function correctly [OK]
Hint: Use setTimeout(func, 0) to schedule next tick [OK]
Common Mistakes:
  • Using setInterval for one-time delay
  • Passing extra argument to setImmediate
  • Confusing process.nextTick with setTimeout syntax
3. What will be the output order of the following code?
console.log('start');
setTimeout(() => console.log('timeout'), 0);
Promise.resolve().then(() => console.log('promise'));
console.log('end');
medium
A. promise
start
end
timeout
B. start
promise
end
timeout
C. start
end
timeout
promise
D. start
end
promise
timeout

Solution

  1. Step 1: Identify synchronous and asynchronous parts

    console.log('start') and console.log('end') run immediately (synchronously). setTimeout callback runs later. Promise callback runs as microtask after current stack.
  2. Step 2: Trace execution order

    Output order: 'start' (sync), 'end' (sync), 'promise' (microtask), 'timeout' (timer callback).
  3. Final Answer:

    start
    end
    promise
    timeout
    -> Option D
  4. Quick Check:

    Synchronous > microtasks > timers [OK]
Hint: Sync logs first, then promises, then timers [OK]
Common Mistakes:
  • Thinking promise runs after timeout
  • Mixing order of synchronous logs
  • Assuming setTimeout runs immediately
4. Consider this code snippet:
setTimeout(() => console.log('timeout'));
process.nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));

Which line causes the earliest callback to run, and why might the output order be unexpected?
medium
A. process.nextTick runs earliest because it runs before microtasks
B. setTimeout runs earliest because timers run first
C. Promise.then runs earliest because promises run before nextTick
D. All callbacks run simultaneously

Solution

  1. Step 1: Understand callback priorities

    process.nextTick callbacks run immediately after the current operation, before promise microtasks and timers.
  2. Step 2: Explain output order

    Even though promises are microtasks, process.nextTick callbacks have higher priority and run first, which can surprise learners expecting promises first.
  3. Final Answer:

    process.nextTick runs earliest because it runs before microtasks -> Option A
  4. Quick Check:

    nextTick > promises > timers [OK]
Hint: nextTick runs before promises and timers [OK]
Common Mistakes:
  • Assuming timers run before nextTick
  • Confusing promise and nextTick order
  • Thinking callbacks run simultaneously
5. You want to run a CPU-heavy task without blocking the event loop in Node.js. Which approach best uses the event loop model to keep your app responsive?
hard
A. Run the task synchronously in the main thread
B. Use setTimeout to split the task into smaller chunks
C. Use process.nextTick to run the entire task immediately
D. Run the task inside a Promise without splitting

Solution

  1. Step 1: Understand event loop blocking

    Running a heavy task synchronously blocks the event loop, making the app unresponsive.
  2. Step 2: Choose non-blocking approach

    Splitting the task into smaller chunks with setTimeout allows the event loop to process other events between chunks, keeping responsiveness.
  3. Final Answer:

    Use setTimeout to split the task into smaller chunks -> Option B
  4. Quick Check:

    Split heavy tasks with timers to avoid blocking [OK]
Hint: Split heavy tasks with setTimeout to avoid blocking [OK]
Common Mistakes:
  • Running heavy tasks synchronously
  • Using nextTick for long tasks (blocks event loop)
  • Assuming promises alone prevent blocking