Bird
0
0

What will be the output order of the following Node.js code?

medium📝 component behavior Q13 of 15
Node.js - Timers and Scheduling
What will be the output order of the following Node.js code?
console.log('start');
setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
process.nextTick(() => console.log('nextTick'));
console.log('end');
Astart, end, nextTick, timeout, immediate
Bstart, end, timeout, nextTick, immediate
Cstart, nextTick, end, immediate, timeout
Dstart, end, nextTick, immediate, timeout
Step-by-Step Solution
Solution:
  1. Step 1: Identify synchronous and asynchronous parts

    console.log('start') and console.log('end') run immediately. process.nextTick runs after current operation but before event loop phases.
  2. Step 2: Understand event loop order

    process.nextTick runs first, then setImmediate callbacks, then setTimeout with 0 delay.
  3. Final Answer:

    start, end, nextTick, immediate, timeout -> Option D
  4. Quick Check:

    nextTick before immediate before timeout [OK]
Quick Trick: nextTick runs before immediate and timeout [OK]
Common Mistakes:
  • Assuming setTimeout runs before setImmediate
  • Thinking nextTick runs after synchronous logs
  • Mixing order of immediate and timeout

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes