1. With only one main thread, node starts executing the script with advanced event loop initialization (synchronous task, issue asynchronous request, plan timer effective time, execute promise.nexttick, etc.), this is the event loop has not started.
2. Nodejs the six stages of each round of event loops (the event loop executes indefinitely until the callback function queue of the asynchronous task is emptied to stop execution):
- Timers (callback function for handling settimeout and SetInterval)
- I/O callbacks (except settimeout, SetInterval, Setimmediate, callback function for closing the request)
- Idle,prepare (LIBUV internal use)
- Poll (Waiting for I/O events that have not yet returned)
- Check (setimmediate)
- Close callbacks (the callback that performs the shutdown request, such as Socket.on (' close ', ...) )
3. settimeout and Setimmediate:
Since the second parameter of settimeout is 0 by default, but with node not being true 0ms, 1s is required at least, so if the actual execution goes into the event loop, if it is not 1ms, then the timers stage will skip to the check stage, so the order of execution is indeterminate.
4. There are two types of asynchronous tasks:
This round loop: Promise.nexttick, Promise callback function
Second wheel cycle: SetTimeout, Setinteval, Setimmediate callback function
Nodejs Event Loop