Order of Execution
- Call Stack: Sync code runs first.
- process.nextTick: Runs immediately after the current operation completes, before the event loop continues. It can starve the I/O loop if abused.
- Microtasks (Promises): Run after
nextTick but before rendering/macrotasks.
- Macrotasks (Timers/IO):
- Timers Phase:
setTimeout
- Poll Phase: I/O callbacks (fs, http)
- Check Phase:
setImmediate
Summary
process.nextTick has the highest priority of async operations. setImmediate runs after I/O callbacks in the "Check" phase, making it distinct from setTimeout(0).