QuizNot attempted
nextTick vs promises vs timers
1–4 to choose, Enter to submit
Show answer & explanation
Correct answer:
- sync, nextTick, promise, then timeout and immediate in either order
- Order is: sync code, then the
process.nextTickqueue (Node drains it before promises), then promise microtasks, then the loop phases. - In the main module
setTimeout(0)vssetImmediateis non-deterministic: it depends on whether the 1ms timer is already due when the loop enters the timers phase. - Not promise before nextTick: nextTick has priority over the promise queue.
- Not nextTick before sync: nothing async runs until the current script finishes.
- Not timers before nextTick/promise: both microtask-style queues drain before any loop phase.