Home  >  Article  >  Web Front-end  >  nodejs visual learning: event loop [animation demonstration]

nodejs visual learning: event loop [animation demonstration]

青灯夜游
青灯夜游forward
2022-11-25 20:56:071784browse

This article will help you learn the Node event loop through animation. I hope it will be helpful to everyone!

nodejs visual learning: event loop [animation demonstration]

We’ve all heard that JavaScript and Node.js are single-threaded, but what does that actually mean?

nodejs visual learning: event loop [animation demonstration]

This means that JavaScript can only do one thing at a time. For example, we cannot multiply and add numbers at the same time. We usually do this in sequence. We add and then multiply and vice versa. Modern computers are so fast that the results of two or more consecutive tasks appear to be calculated simultaneously, but there are exceptions. [Related tutorial recommendations: nodejs video tutorial]

We have all tried to scrape data from that slow website, or waited more than 30 seconds before getting the database query results. Do we want to prevent a single thread from performing more tasks because the database query is slow? However, Node.js does not stop running other operations, because Libuv is a C library that is responsible for event loops and asynchronous processing of network requests, DNS resolution, file system operations, data encryption, and other tasks.

What happens behind the scenes when Node.js handles tasks like database queries? We will explore it step by step by following this code.

This is our sample code

nodejs visual learning: event loop [animation demonstration]

The V8 JavaScript engine manages the call stack, which is what keeps track of which part of our program is running important part. Whenever we call a JavaScript function, it is pushed up the call stack. Once the function reaches its end or return statement, it is popped off the stack.

In our example, the line console.log('Starting Node.js') is added to the call stack and prints Starting Node.js to the control tower. By doing this, it reaches the end of the log function and is removed from the call stack.

Node.js 调用堆栈上的函数调用

The following line of code is a database query. These tasks pop up immediately because they can take a long time. They are passed to Libuv, which processes them asynchronously in the background. At the same time, Node.js can continue running other code without blocking its single thread.

In the future, Node.js will know how to handle queries because we have associated callback functions with instructions for handling task results or errors. In our case it's a simple console.log, but it could be complex business logic or data processing in a production application.

Libuv 处理 I/O 操作

When Libuv processes the query in the background, our JavaScript is not blocked and can continue to use console.log(”Before query result”).

在 Node.js 运行我们的代码时处理 I/O

After the query completes, its callback will be pushed to the I/O event queue to run immediately* . * The event loop connects the queue to the call stack. It checks if the latter is empty and moves the first queue item for execution.

nodejs visual learning: event loop [animation demonstration]

The code can be found at github.com/fabrillallo…

Quiz on the Event Loop

Try to find out the following code printed on the console.

nodejs visual learning: event loop [animation demonstration]

##Conclusion

Event loops, delegates and asynchronous processing mechanisms are the key to Node.js handling thousands of connections, reads The secret ingredient to fetching/writing huge files, handling timers while working on other parts of our code.

In the article, we saw the important role of Libuv and its ability to handle many potentially long-running tasks. At the same time, we learned about the event loop and its role as a bridge/connector between asynchronous operation callbacks and the call stack in the I/O event queue. In the following articles, we will explore in more detail how the different stages of the event loop handle

timers, I/O, promises, and ticks .

English original address: https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62

For more node-related knowledge, please visit:

nodejs tutorial!

The above is the detailed content of nodejs visual learning: event loop [animation demonstration]. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete