Understand Node's event loop in one article

青灯夜游
Release: 2023-04-06 18:39:26
forward
2468 people have browsed it

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!

Understand Node's event loop in one article

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.

1. What is the event loop?

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:

##Nodejs is like a smart kid, based on Js It cannot implement multi-threaded operations by itself, but it leaves multi-threaded operations to the system kernel.

#Because most of the system kernels are multi-threaded, and the kernel is not so easy to execute, then Nodejs can sit back and relax?

2. Why do we need an event loop?

(First explain the name of event loop. Nodejs is event-driven. When and when to do something, what is done is defined in the callback function;

Therefore, the callback function can be defined It is an event processing function; so the mechanism for managing callback functions is called an event loop;)

Since the kernel is so powerful, can Nodejs be light-hearted?

No, after the kernel execution is completed, Nodejs must execute the corresponding callback function.

So a mechanism is needed to help it manage and maintain these asynchronous operation callback functions to prevent them from fighting and random strings.

Thus executed in an efficient manner. So that's why ---

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:

Simple understanding, for the above three asynchronous APIs, the event loop provides three types of queues,

strangeness? Why is there no process.nectTick?

Haha, that’s because process.nectTick itself is weird!

process.nectTick

Although process.nectTick belongs to the asynchronous API, it is not part of the event loop.

Above picture:

This involves another concept:

Asynchronous module!

Asynchronous module

is this magical guy. nodejs uses the

libuvlibrary 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!

? Tip 2: process.nectTick is the fastest among all asynchronous tasks;

(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?

Promise

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:

? Tip 3: The microtask queue is appended behind the process.nectTick queue and in front of the event loop;

So much has been said above, let’s look at it as a whole

? Tip 4: Practice brings true knowledge;

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');})复制代码
Copy after login

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!