Non‑deterministic execution order of setImmediate() and setTimeout(0) in top‑level Node.js scripts
0 reputation · 09 Apr 2023, 01:20 UTC
Determine whether Node.js guarantees that setImmediate() callbacks run before setTimeout(fn,0) when both are scheduled from the synchronous top‑level scope, outside any I/O phase.
The event‑loop specification defines the check and timer phases but does not fix their relative priority for callbacks queued in the main module, and recursive process.nextTick() calls can prevent the loop from advancing to the check phase, making the outcome dependent on implementation details.
What is the defined execution order of setImmediate() versus setTimeout(0) in a non‑I/O context? Does recursive process.nextTick() block the event loop from reaching the check phase? Which ordering pattern emerges when both mechanisms are used in the same tick?