When I first saw the event-loop, I was confused. What is this? What kind of loop is it?

There are some differences between the event ring in the browser and the NodeJS environment, and I’m only looking at the small black box event ring in the NodeJS environment.

The event loop here does not refer to a single event loop, but rather we write many, many events to execute in a queue according to certain rules, and then after the queue emptying again, is the event loop.

The event loop is very complex, and HERE I only have the ability to explain a few points in the event loop:

  • An explanation of event loops in Node.js
  • Macro task, micro task

An explanation of event loops in Node.js

Nodejs divides eventloop into:

  • Timers: setTimeout Is executed to add callback to the queue.
  • Pending callbacks: Some I/O callback that is deferred until the next loop.
  • Idle, prepare: indicates some internal events.
  • Poll: Timer callback execution, setImmediate execution, microtask execution.
  • Check: setImmediate callback execution.
  • Close Callbacks: Closure of some callbacks, such as sockets.

Here, we focus on the three stages of timers, Poll and check. We don’t use much of the others.

Timers, Poll, and Check stages

  • timers

At this stage, only setTimeout and setInterval are executed, but their callback is not executed, instead being pushed to the queue of macro tasks.

  • poll

Through this phase, the conditional microtask, such as the Promise’s asynchronous completion, is performed. SetImmediate mediate does not perform its callback, and then the timer’s callback, such as timeout, is performed. There will be an appropriate pause to see if any new tasks enter the queue. The setImmediate callback enters the Check phase if there is a setImmediate callback, otherwise it returns to the Timer to continue the loop.

  • check

When the queue for the poll phase is complete, the check turns, and the setImmediate callback is executed. If there is no need to close the callbacks, go back to the timer and continue the cycle.

Macro tasks vs. micro tasks

  • Macro task

From my perspective, a normal task can run one after another in a thread without any problems, but each macro task is likely to generate some micro tasks after the execution, so unfortunately, these macro tasks will come after these micro tasks.

Macro task representatives: Script (overall code),setTimeout,setImmediate.

/** Output: I'll go first. You are too slow. I'll cut you insetTimeout(()=>{
    console.log("I'll go first.")})setTimeout(()=>{
    console.log("Old driver, wait for me.")
},10)
setImmediate(()=>{
    console.log("You're too slow. I'll cut in.")})Copy the code

To highlight

SetTimeout and setImmediate mediate trigger different stages, so callback execution time is different. However, if setTimeout is too long, setImmediate does not mediate. Then, in the next round of polling, the system runs the Callbacks of setTimeout if setTimeout runs out.

  • Micro tasks

New small tasks, such as asynchronous tasks, are generated during the execution of macro tasks. Such tasks are called micro tasks and are generally executed after the execution of the current macro task.

Microtask representatives: Process. nextTick, Promise(native).

To highlight

Although process.nextTick and Promise are microtasks, they are executed in different order. No matter whose code executes first, nextTick executes before Promise when both are runnable during the poll phase.

/ / promise.resolve ().then()=>{console.log() {console.log();"One day, I'm gonna be in charge.") 
})
process.nextTick(()=>{
    console.log("I'm always more than you could ever hope to be.")})Copy the code

Postscript:

I only wrote about my understanding of Eventloop, but there are a lot of cloudy areas, and I only wrote about my understanding.