Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 5, 2022 06:15 pm GMT

An animated guide for Node.js event loop

We have all heard about JavaScript and Node.js being single-threaded, but what does it mean in practical terms?

It means that JavaScript can do one thing at a time. For example, we cannot simultaneously multiply and sum numbers. We usually do operations in sequence. We add and then multiply or vice versa. Modern computers are fast, and the result of two or more consecutive tasks seems to be computed simultaneously, but there are exceptions.

We all have tried to scrape data from that slow website or waited more than thirty seconds before getting the result of a database query. Do we want to block our single thread from executing more tasks because of a slow database query? Luckily, Node.js doesnt stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.

Code sample to showcase the event loop

The V8 JavaScript engine manages a call stack, an essential piece that tracks which part of our program is running. Whenever we invoke a JavaScript function, it gets pushed to the call stack. Once the function reaches its end or a return statement, it is popped off the stack.

In our example, the line of codeconsole.log('Starting Node.js')is added to the call stack and printsStarting Node.jsto the console. By doing so, it reaches the end of the log function and is removed from the call stack.

Function invocation on Node.js call stack

The following line of code is a database query. These tasks are immediately popped off because they may take a long time. They are passed to Libuv, whichasynchronouslyhandles them in the background. At the same time, Node.js can keep running other code without blocking its single thread.

In the future, Node.js will know what to do with the query because we have associated a callback function with instructions to handle the task result or error. In our case, it is a simpleconsole.log, but it could be complex business logic or data processing in production applications.

Libuv handles I/O ops

While Libuv handles the query in the background, our JavaScript is not blocked and can continue withconsole.log(Before query result).

Processing I/O while Node.js runs our code

When the query is done, itscallback is pushed to theI/O Event Queue to be run shortly*.*The event loop connects the queue with the call stack. It checks if the latter is empty and moves the first queue item for execution.

The event loop checks for an empty call stack

Pop quiz on the event loop

Try to figure out what the following code prints on the console.

A more complicated code sample to showcase the event loop

An animated guide for the event loop

Conclusion

The event loop, the delegation, and the asynchronous processing mechanism are Node.js's secret ingredients to process thousands of connections, read/write gigantic files, handling timers while working on other parts of our code.

In the article, we saw the vital role of Libuv and its ability to handle numerous potentially long-running tasks. At the same time, we went through the event loop and its role as a bridge/connector between callbacks of asynchronous operations in the I/O event queue and the call stack.In the following articles, we will explore in greater detail how timers, I/O, promises, and ticks are handled by the different phases of the event loop.

If you liked the article, follow us on Twitter @fabriziolallo and @andrewhu368


Original Link: https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To