Learned some about Node.js internals and Callback Queues in particular. It appeared Node.js maintains multiple prioritized queues to handle callbacks.
Call Stack - keeps track of the function currently being executed and of its origin. Event Loop - responsible for code execution. Callback Queues - hold callback functions of completed asynchronous operations.
In order of priority
- Microtask queue -
process.nextTick,Promise - Timer queue -
setTimeout,setInterval - IO queue - file system, network
- Check queue -
setImmediate - Close queue - closing HTTP connection, stream, etc
Links