Node.js has its own event loop, but unlike the browser, there are five macro task queues in Node.js:
- Timer queue, which holds callbacks added by setTimeout and setInterval functions
- System-related callback queues, such as port listening and process resource usage
- IO event queues, which hold I/O event callbacks, are key to Node.js asynchronous non-blocking
- Immediately executes the queue that stores the callback added by the setImmediate function
- Close the action queue, which holds the close event processing callback
In addition to the above five macro task queues, there are also two microtask queues, which are:
- NextTick callback queue, the callbacks added using the process.nexttick () function
- Other microtask queues, columns such as Promise, queueMicrotask, and so on
A few notes:
- There is more than one queue in NodeJS, and different types of events are enqueued in their own queues.
- Each macro task phase emptying the tasks of that phase before moving to the next phase emptying two microtask queues
- The nextTick microtask queue has the highest priority. Clear the nextTick callback queue first and then other microtask queues
- Once the other microtask queues have been emptied, check the nextTick callback queue again to make sure it has been emptied
The illustration is as follows: