This article will help you learn the Node event loop through animation. I hope it will be helpful to everyone!
We’ve all heard that JavaScript and Node.js are single-threaded, but what does that actually mean?
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
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.
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.
When Libuv processes the query in the background, our JavaScript is not blocked and can continue to use console.log(”Before query result”)
.
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.
The code can be found at github.com/fabrillallo…
Try to find out the following code printed on the console.
timers, I/O, promises, and ticks .
English original address: https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62For 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!