This article talks about the event loop in Nodejs. I hope to help everyone understand the event loop in Nodejs. From now on, you will no longer be afraid of the soul of the interviewer asking: Talk about the event loop of Nodejs!
I think everyone will be asked by the interviewer during the interview: "Let's talk about the event loop of Nodejs."
Because I have also been asked this question, but every time it is embarrassing.
There are many introductions to this issue on various technical blogs, but I have never understood it. Because these articles often start with a lot of diagrams and terminology, which instantly extinguishes the courage to understand. [Recommended related tutorials:nodejs video tutorial,Programming teaching]
But you can’t understand it, the interviewer still has to ask , so with tears and gritted teeth, I consulted some tutorials, came up with my own summary, and immediately shared it with everyone.
In a word: The event loop is the mechanism for Nodejs to handle asynchronous operations.
Js is single-threaded, why can Nodejs handle asynchronous operations?
Because Nodejs hands over multi-threaded operations to the system kernel.
Above picture:
Event Loop--- is needed.
Summary: The event loop is used by Nodejs to control the execution order of asynchronous code callbacks!
3. How to understand the event loop? ? Tip 1: Synchronous tasks are always executed earlier than asynchronous tasks; Asynchronous API classification Needless to say more about synchronous tasks, here we first introduce the asynchronous API in Nodejs Classification:Asynchronous module!
Asynchronous modulelibuvlibrary to call the kernel to implement multi-threaded operations. !
How much does this have to do with process.nectTick?Yes, because process.nectTick can be understood as part of the asynchronous module.
Therefore, process.nectTick will always be called before the event loop!
(Note: Understand the Tick
event Loop through the three queues to run for a week and become a Tick!)
Okay, got it!
Wait...it seems like something is missing? How is Promise executed?
In addition to the nextTick queue, there is also a special queue: the microtask queue. The microtask queue is mainly used to handle the execution of Promise callback functions.
What is the execution order of the microtask queue?
Above picture:
So much has been said above, let’s look at it as a whole
Use the above theory, if you are smart, you can analyze it What is the final printing order?
console.log('同步代码')setImmediate(() => { console.log('setImmediate');})setTimeout(() => { console.log('setTimeout');}, 100)Promise.resolve().then(() => { console.log('promise');})process.nextTick(() => { console.log('Tick');})复制代码
Then next time we will combine specific interview cases to see what their printing order is.
For more node-related knowledge, please visit:nodejs tutorial!
The above is the detailed content of Understand Node's event loop in one article. For more information, please follow other related articles on the PHP Chinese website!