简体   繁体   中英

Node.js Event Loop Phases

I am new to node.js, I have gone through the event loop documentation given on node.org.

There it is stated that : When Node.js starts, it initializes the event loop, processes the provided input script (or drops into the REPL, which is not covered in this document) which may make async API calls, schedule timers, or call process.nextTick(), then begins processing the event loop.

I have read the documentation for every phase, but could not understand completely.

Lets say I have following code in index.js:

console.log('started...');
setTimeout(function(){
    console.log('timeout callback...');
},1000);
console.log('Finishes...');

I want to know in which phase my entire code first be loaded, and how event loop will be used for above code when I run command

node index.js

Thanks.

Ok, you should see started ..then Finishes statements followed by the timeout callback .

As the doc stated above the eventLoop make async API calls ie not waiting for next phase to call while executing the setTimeout function (using Node Timers ). Nodejs waits for 1000 milliseconds for the callback to be completed with another THREAD and finishes runs earlier.

The demultiplexer is a notification-issuing interface within the Node JS. It is used to gather every request from watched sources in form of an event and queues each event in a queue. It is the demultiplexer that forms the Event Queue . Event demultiplexer is an API run by Libuv.

A queue is a sequentially ordered data structure that uses the first in, first out (FIFO) principle: items are removed from a queue in the order in which they were inserted. If there are no events in the event queue or the Event Demultiplexer has no pending requests, the program will complete.

Here is a diagram of formed event queues by event demultiplexer (by libuv). 在此处输入图片说明

The queue that on top has priority over the below queue. In your code snippet setTimeout function will be passed to timers queue(it is a different data structure but lets use queue) and will take 1000 ms to be executed.

So event loop looks at timers first, since there is nothing to be moved to call stack where functions go to run, event loop will check the 2 other queues which are part of the node itself. So, before jumping to next libuv queue, node first will check those 2 queues which are

Next Ticks Queue — Callbacks added using process.nextTick function Other Microtasks Queue — Includes other microtasks such as resolved promise callbacks.

2 console functions will be pending in other microtasks queue. So event loop will pass those functions to call stack to be executed.

Started

Finishes

will be logged to console. Since there is nothing else to do, event loop will wait till 1000 ms is up and then will pass the setTimeout function to the call stack to be executed. So this is what you will see on console.

Started
Finishes
timeout callback...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM