Node Event Loop Phases: Where Your setTimeout Really Runs

setTimeout zero and setImmediate are not the same thing, and which one runs first depends on where in the event loop you called them.

The Node Event Loop Has Phases, and Your setTimeout Is in the Wrong One. Abstract deep dive illustration in orange and dark grey on debugly.dev
Phase Drains
timers callbacks scheduled by setTimeout and setInterval whose deadline arrived
pending callbacks deferred system callbacks, mostly I/O errors
idle, prepare internal
poll new I/O events, and where the loop blocks waiting for I/O
check callbacks scheduled by setImmediate
close callbacks socket and handle close events

Published on debugly.dev by Rohit Bhadani | Founder of Krova Cloud