Demystifying The Concept Of Node.js Event Loop

Demystifying The Concept Of Node.js Event Loop

Table Of Contents

  • Introduction
  • Definition Of Keywords
  • What is Event Loop?
  • Conclusion

Introduction

This article is mainly targeted at beginners and Intermediate developers who still kind of find it difficult to grasp one of the more obscure aspects of programming in general, and Nodejs in particular. The term "event loop" in programming has always piqued the interest of some of us, including myself. It compelled me to conduct more research to learn more. Allow me to take you on a tour of my research. In the next section, we will define keywords/terms that will be commonly used throughout this article to keep us abreast of the flow of the article. Please stay with me.

Definition of Keywords

  • Demystifying: “ To make something easier to understand by giving a clear explanation of it.” - The Collins Dictionary
  • Concept: An Idea -The Collins Dictionary
  • Node.js: A JavaScript runtime environment built on Chrome v8 engine for writing server-side logic
  • Event: An occasion or party in layman’s term
  • Loop: A curved or circular shape in something long, for example in a piece of string. - The Collins Dictionary

So, taking all of these words together, we'll be talking about "making it easier to understand the concept of a Javascript runtime environment built on the Chrome v8 engine as it relates to a circular or recurring event." Let's look at the overview of Nodejs and what it is.

What is Event Loop?

Runtime in Nodejs is defined as the period between when our code is executed and when it is closed or quits. It accomplishes this by keeping track of each function that is executed, and it does so by employing a technique known as "the call stack."

When a function is called and executed, the call stack is in charge of removing it from the stack.

"How do these functions get sent to this call stack in the first place?" you may be wondering. To address this, we'll look at another keyword called "Event Queue."

An Event Queue is a message queue that contains a list of messages that point to each function and is in charge of maintaining the order in which these functions are sent to the call stack as well as the correct sequence of execution when it comes to each function's turn.

A typical example is a script with setTimeOut and console.log() functions. Both enter the queue, but the setTimeOut function is routed to the Browser API, which operates as a separate thread. The console.log is executed and popped out of the call stack while the setTimeOut functions wait for it to time out. Once the setTimeOut function has timed out, it is returned to the call stack.

After all the functions have been called, the event loop checks to see if the stack is empty, and if it is, it pushes in the next message to be executed. The procedure is repeated.

Conclusion

Here are more resources, in any case, you are inquisitive to research more on your own.

https://www.educative.io/edpresso/what-is-an-event-loop-in-javascript

https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

I look forward to hearing your thoughts in the comment section. Thanks for reading