The first time I saw him in the Ring of events (Event-loop), I was a face Meng, what is this ghost, what is the loop, why the event also loop, not all a one-time?
There are some differences between the event loops in the browser and the NODEJS environment, and here I only study the event loops in the case of the NODEJS environment, the small black box.
The event loop here does not refer to a single event loop, but rather many of the events we write are queued in accordance with certain rules, and then the queue is emptied and queued, which is the event loop.
The event loop is complex, and here I have the ability to interpret several points in the event loop:
- Explanation of event Loops in node. js
- Macro Task (Macro-task), Micro Task (Micro-task)
Explanation of event Loops in node. js
Nodejs the EventLoop into:
- Timers: The timer settimeout executes and the callback is added to the queue.
- Pending callbacks: Some I/O callback, deferred to the next cycle of execution.
- Idle, prepare: some of the internal events.
- Poll: Callback execution of the timer, setimmediate execution, micro-task execution.
- Check:setimmediate's callback execution.
- Close callbacks: Some callbacks are closed, such as sockets.
Here we focus on the three stages of timers, poll and check. We don't use much of the rest.
Timers, poll, check stage
At this stage, only settimeout and setinterval are executed, but their callback are not executed but are pushed into the queue of the macro task.
At this stage, the prerequisite micro-task, such as promise asynchronous completion, if it is setimmediate, will only execute, do not execute his callback, and then execute the callback of the timer, such as timeout. It's a good place to pause for a while and see if there's a new task going into the queue. If there is a setimmediate callback to enter the check stage, or return to the timer to continue a new round of cycle.
When the queue for the poll phase is complete, the check is performed, and the Setimmediate callback is executed. If you do not need to close callbacks, then go back to the timer to continue a new round of loops.
Macro Tasks vs Micro tasks
From my point of view, is a normal task, originally in a thread can be no twists and turns one after another to run to the end, but every macro task can produce some micro-task after execution, so unfortunately, these tasks will be ranked in these micro-tasks.
Macro Task representative: script (whole code), Settimeout,setimmediate.
/**output:我先走一步你太慢了,我插个队老司机,等等我*/setTimeout(()=>{ console.log("我先走一步") })setTimeout(()=>{ console.log("老司机,等等我")},10)setImmediate(()=>{ console.log("你太慢了,我插个队")})
Focus
SetTimeout and Setimmediate, the triggering stages are different, so callback execution times are different. But if the settimeout time is too long, then the system will execute setimmediate first, then wait for the next poll, if settimeout to time, then run settimeout callbacks.
When a macro task is executed, a new small task, such as asynchronous, is called a micro-task, which is usually executed after the current macro task has been executed.
Micro Mission Representative: Process.nexttick, Promise (native).
Focus
Although both Process.nexttick and promise are micro-tasks, the sequencing of their execution is not the same. No matter whose code executes first, until the poll phase, both are operational states, Nexttick is performed before promise.
/**output:本宫始终是你望成莫及的总有一日,我会上位*/Promise.resolve().then(()=>{ console.log("总有一日,我会上位") })process.nextTick(()=>{ console.log("本宫始终是你望成莫及的") })
Postscript:
I only wrote about my understanding of eventloop, but there are a lot of foggy, and it's just that I understand.