This website requires JavaScript.
Eventloop image

Event Loop in Nodejs

Understand the core nature of the most important mechanism in Nodejs

2 min read

I. What is Event Loop?

  • JavaScript is a single-threaded language, which means it can only execute one task at a time.
  • Event Loop is a mechanism in JavaScript Runtime, which helps manage and process events, tasks, and asynchronous operations efficiently without blocking the main thread. It is the core nature of JavaScript that allows it to handle multiple tasks simultaneously.

II. Components of Event Loop

  • Call Stack: A data structure that stores the function calls in the order they are called. When a function is called, it is pushed into the Call Stack. When the function is done, it is popped out of the Call Stack.
  • Callback Queue (Task Queue): A data structure that stores the callback functions that are ready. They include: setTimeout/setInterval, callback events (onscroll, onclick, ...), ...
  • Event Loop: A loop that continuously checks the Call Stack and the Callback Queue.
  • Microtask Queue: A queue that stores microtasks. Microtasks are tasks that are executed immediately after the current task and before the next task in the Call Stack. They include: Callbacks from Promises (then, catch, finally), Callbacks from MutationObserver, queueMicrotask, ...
  • Macrotask Queue: A queue that stores macrotasks. Macrotasks are tasks that are executed after the current task and after all the microtasks in the Microtask Queue.
  • Web APIs: Web APIs are APIs provided by the browser that allow you to interact with the browser environment. For example, the DOM API, setTimeout, setInterval, XMLHttpRequest, fetch, etc.
  • Promises: Promises are objects that represent the eventual completion (or failure) of an asynchronous operation and its resulting value.
  • Async/Await: Async/Await is a syntactic sugar for Promises. It allows you to write asynchronous code that looks synchronous.
  • setTimeout/setInterval: Functions that allow you to execute a function after a specified amount of time or at regular intervals.
  • Events: Events are actions or occurrences that happen in the system. For example, clicking a button, scrolling a page, submitting a form, etc.
  • I/O Operations: Input/Output operations such as reading/writing files, making network requests, etc.

Javascrip Runtime

III. How does it work?

  • Event Loop is a loop that continuously checks the Call Stack and the Callback Queue.
  • When the Call Stack is empty, the Event Loop will take the first task in the Callback Queue and push it into the Call Stack to execute.
  • The Call Stack is a data structure that stores the function calls in the order they are called. When a function is called, it is pushed into the Call Stack. When the function is done, it is popped out of the Call Stack.
  • Tags:
  • nodejs
  • javascript
Jan 17, 2025
Previous page
9 lines of JavaScript replace 50 lines of code
Next page
Classification and functionality of the servers

🍃 Related posts