Bird
0
0

Why does this code never print 'Done'?

medium📝 Debug Q7 of 15
Node.js - Timers and Scheduling
Why does this code never print 'Done'?
setTimeout(() => {
  while(true) {}
  console.log('Done');
}, 0);
Aconsole.log is inside while loop and never reached
BsetTimeout with 0 delay never runs callbacks
CInfinite loop blocks event loop, so callback never finishes
DSyntax error in callback function
Step-by-Step Solution
Solution:
  1. Step 1: Analyze infinite loop inside callback

    The while(true) loop runs forever, blocking the event loop thread.
  2. Step 2: Understand event loop blocking

    Because the callback never finishes, console.log('Done') is never executed.
  3. Final Answer:

    Infinite loop blocks event loop, so callback never finishes -> Option C
  4. Quick Check:

    Blocking infinite loop stops event loop progress [OK]
Quick Trick: Infinite loops block event loop, stopping all callbacks [OK]
Common Mistakes:
  • Thinking 0 delay prevents callback
  • Assuming console.log runs inside loop
  • Confusing syntax error with runtime block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes