0
0
Node.jsframework~20 mins

Event loop phases and timer execution in Node.js - Practice Problems & Coding Challenges

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