Bird
0
0

Consider this code:

medium📝 Predict Output Q5 of 15
Node.js - Timers and Scheduling
Consider this code:
process.nextTick(() => console.log('A'));
setImmediate(() => console.log('B'));
process.nextTick(() => console.log('C'));
setImmediate(() => console.log('D'));
What is the correct order of output?
AA, C, B, D
BB, D, A, C
CA, B, C, D
DC, A, D, B
Step-by-Step Solution
Solution:
  1. Step 1: Group callbacks by type

    All process.nextTick callbacks run before setImmediate callbacks.
  2. Step 2: Maintain order within groups

    process.nextTick callbacks run in the order they were scheduled: 'A' then 'C'. Then setImmediate callbacks run in order: 'B' then 'D'.
  3. Final Answer:

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

    NextTick then Immediate order = B [OK]
Quick Trick: NextTick callbacks run before all setImmediate callbacks [OK]
Common Mistakes:
  • Mixing the order of nextTick callbacks
  • Assuming setImmediate runs first
  • Ignoring callback scheduling order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes