Bird
Raised Fist0
Node.jsframework~20 mins

Event loop phases and timer execution 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!
🧠 Conceptual
intermediate
2:00remaining
Understanding setTimeout and setImmediate order
Consider this Node.js code snippet:
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));

What is the most likely output order when this code runs?
Node.js
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
Atimeout only
Bimmediate\ntimeout
Ctimeout\nimmediate
Dimmediate only
Attempts:
2 left
💡 Hint
Think about which phase runs first after the current poll phase.
component_behavior
intermediate
2:00remaining
Behavior of process.nextTick in event loop
What happens when you schedule a callback with process.nextTick inside a setTimeout callback?
setTimeout(() => {
console.log('timeout');
process.nextTick(() => console.log('nextTick inside timeout'));
}, 0);

What will be the output order?
Node.js
setTimeout(() => {
  console.log('timeout');
  process.nextTick(() => console.log('nextTick inside timeout'));
}, 0);
Atimeout\nnextTick inside timeout
BnextTick inside timeout\ntimeout
COnly timeout
DOnly nextTick inside timeout
Attempts:
2 left
💡 Hint
Remember when nextTick callbacks run relative to the current phase.
📝 Syntax
advanced
2:00remaining
Identify error in timer callback usage
Which option will cause a runtime error when executed in Node.js?
setTimeout(() => console.log('Hello'), '1000');
Node.js
setTimeout(() => console.log('Hello'), '1000');
AReferenceError because setTimeout is undefined
BTypeError because delay must be a number, not string
CSyntaxError due to arrow function
DNo error, prints 'Hello' after 1 second
Attempts:
2 left
💡 Hint
Check how Node.js treats string delays in setTimeout.
🔧 Debug
advanced
2:00remaining
Why does this setInterval never stop?
Consider this code:
let count = 0;
setInterval(() => {
if (count === 3) clearInterval();
console.log(count);
count++;
}, 1000);

Why does the interval never stop?
Node.js
let count = 0;
const intervalId = setInterval(() => {
  if (count === 3) clearInterval(intervalId);
  console.log(count);
  count++;
}, 1000);
AclearInterval is called without interval ID, so it does nothing
Bcount never reaches 3 because of async behavior
CSyntax error due to missing semicolon
DsetInterval is not cleared because count is reset
Attempts:
2 left
💡 Hint
What does clearInterval need to stop an interval?
lifecycle
expert
3:00remaining
Order of phases with nested timers and nextTick
What is the output order of this Node.js code?
console.log('start');
setTimeout(() => {
console.log('timeout 1');
process.nextTick(() => console.log('nextTick inside timeout 1'));
}, 0);
setImmediate(() => {
console.log('immediate 1');
setTimeout(() => console.log('timeout 2'), 0);
});
process.nextTick(() => console.log('nextTick 1'));
console.log('end');
Node.js
console.log('start');
setTimeout(() => {
  console.log('timeout 1');
  process.nextTick(() => console.log('nextTick inside timeout 1'));
}, 0);
setImmediate(() => {
  console.log('immediate 1');
  setTimeout(() => console.log('timeout 2'), 0);
});
process.nextTick(() => console.log('nextTick 1'));
console.log('end');
Astart\nend\nnextTick 1\ntimeout 1\nnextTick inside timeout 1\nimmediate 1\ntimeout 2
Bstart\nend\nnextTick 1\nimmediate 1\ntimeout 1\nnextTick inside timeout 1\ntimeout 2
Cstart\nend\nimmediate 1\nnextTick 1\ntimeout 1\nnextTick inside timeout 1\ntimeout 2
Dstart\nend\nnextTick 1\ntimeout 1\nimmediate 1\nnextTick inside timeout 1\ntimeout 2
Attempts:
2 left
💡 Hint
Remember process.nextTick runs immediately after current operation, and setImmediate runs after poll phase.

Practice

(1/5)
1. Which phase of the Node.js event loop executes setTimeout callbacks?
easy
A. Check phase
B. Timers phase
C. Poll phase
D. Close callbacks phase

Solution

  1. Step 1: Understand event loop phases

    The Node.js event loop has multiple phases, each handling different types of callbacks.
  2. Step 2: Identify where timers run

    The timers phase is specifically designed to execute callbacks scheduled by setTimeout and setInterval.
  3. Final Answer:

    Timers phase -> Option B
  4. Quick Check:

    Timers phase = setTimeout callbacks [OK]
Hint: Timers run in the timers phase, not immediately [OK]
Common Mistakes:
  • Confusing timers phase with poll phase
  • Thinking setTimeout runs immediately
  • Mixing check phase with timers phase
2. Which of the following is the correct syntax to schedule a function to run after 0 milliseconds in Node.js?
easy
A. setTimeout(console.log('Hi'))
B. setTimeout(console.log('Hi'), 0);
C. setTimeout(0, () => console.log('Hi'));
D. setTimeout(() => console.log('Hi'), 0);

Solution

  1. Step 1: Check function syntax for setTimeout

    The first argument must be a function, not the result of a function call.
  2. Step 2: Analyze each option

    setTimeout(() => console.log('Hi'), 0); passes a function that logs 'Hi' after 0 ms delay correctly. setTimeout(console.log('Hi'), 0); calls console.log immediately and passes its result (undefined). setTimeout(0, () => console.log('Hi')); passes 0 (number) as first argument instead of a function, causing a TypeError on execution. setTimeout(console.log('Hi')) calls console.log immediately without delay argument.
  3. Final Answer:

    setTimeout(() => console.log('Hi'), 0); -> Option D
  4. Quick Check:

    Function as first argument = setTimeout(() => console.log('Hi'), 0); [OK]
Hint: Pass a function, not a function call, to setTimeout [OK]
Common Mistakes:
  • Calling the function immediately inside setTimeout
  • Omitting the delay argument
  • Passing non-function as first argument
3. What will be the output order of the following code?
console.log('Start');
setTimeout(() => console.log('Timeout 1'), 0);
setTimeout(() => console.log('Timeout 2'), 10);
console.log('End');
medium
A. Start, End, Timeout 1, Timeout 2
B. Start, Timeout 1, Timeout 2, End
C. Timeout 1, Timeout 2, Start, End
D. Start, Timeout 2, End, Timeout 1

Solution

  1. Step 1: Identify synchronous and asynchronous parts

    console.log('Start') and console.log('End') run immediately in order. setTimeout callbacks run later.
  2. Step 2: Understand timer delays and event loop

    setTimeout with 0 ms delay runs after current code finishes, before 10 ms delay callback.
  3. Final Answer:

    Start, End, Timeout 1, Timeout 2 -> Option A
  4. Quick Check:

    Synchronous logs first, then timers by delay [OK]
Hint: Synchronous logs run before any setTimeout callbacks [OK]
Common Mistakes:
  • Assuming 0 ms timeout runs immediately
  • Mixing order of synchronous and asynchronous logs
  • Ignoring timer delays
4. Identify the error in this code snippet:
setTimeout(console.log('Hello'), 1000);
medium
A. The delay argument is missing
B. The delay must be 0 or less
C. console.log is called immediately instead of after 1000ms
D. setTimeout requires a string as first argument

Solution

  1. Step 1: Analyze the first argument of setTimeout

    console.log('Hello') is called immediately, returning undefined, which is passed to setTimeout.
  2. Step 2: Understand correct usage

    setTimeout expects a function as first argument, not the result of a function call.
  3. Final Answer:

    console.log is called immediately instead of after 1000ms -> Option C
  4. Quick Check:

    Function call inside setTimeout runs immediately [OK]
Hint: Pass a function, not a function call, to setTimeout [OK]
Common Mistakes:
  • Thinking delay argument is missing
  • Passing string instead of function
  • Believing delay must be zero or negative
5. Consider this code:
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
What is the correct order of output?
hard
A. A, D, C, B
B. A, B, D, C
C. A, D, B, C
D. A, C, D, B

Solution

  1. Step 1: Identify synchronous, microtask, and timers

    console.log('A') and console.log('D') run immediately. Promise.then callbacks run in microtasks after current code. setTimeout callbacks run in timers phase later.
  2. Step 2: Determine execution order

    Output order is synchronous logs first (A, D), then microtasks (C), then timers (B).
  3. Final Answer:

    A, D, C, B -> Option A
  4. Quick Check:

    Microtasks run before timers [OK]
Hint: Promise.then runs before setTimeout even with 0 delay [OK]
Common Mistakes:
  • Assuming setTimeout runs before Promise.then
  • Mixing synchronous and asynchronous order
  • Ignoring microtask queue priority