Bird
0
0

Consider this Node.js code snippet:

medium📝 Debug Q14 of 15
Node.js - Timers and Scheduling
Consider this Node.js code snippet:
setTimeout(() => console.log('timeout'));
setImmediate(() => console.log('immediate'));
process.nextTick(() => console.log('nextTick'));
process.nextTick(() => console.log('another nextTick'));

Which issue causes unexpected order of output, and how to fix it?
Aprocess.nextTick callbacks run before setImmediate and setTimeout; no fix needed, it's expected.
BsetImmediate runs before process.nextTick; swap their calls to fix.
CMissing delay in setTimeout causes error; add delay 0 to fix.
DsetTimeout and setImmediate run simultaneously causing race; use only one.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze order of callbacks

    process.nextTick callbacks always run before setImmediate and setTimeout, regardless of order in code.
  2. Step 2: Understand if this is an issue

    This behavior is by design in Node.js event loop; no error or fix needed.
  3. Final Answer:

    process.nextTick callbacks run before setImmediate and setTimeout; no fix needed, it's expected. -> Option A
  4. Quick Check:

    nextTick always runs first [OK]
Quick Trick: nextTick always runs before timers and immediate [OK]
Common Mistakes:
  • Thinking missing delay in setTimeout causes error
  • Expecting setImmediate to run before nextTick
  • Trying to fix timing by swapping calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes