Explain the Mechanism of event loop in Node.js Last Updated : 26 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report JavaScript is a single-threaded, non-blocking, asynchronous language that uses the concept of event loop to make it work asynchronously even if it is single-threaded. Feature: The Event Loop is responsible for sending functions from the event queue to the Stack for processing when it becomes empty. Mechanism of Event Loop: There are some functions in javascript which can run on the browser API instead of on V8. The functions or call which is not present on V8 are sent to the browser API and then work as a single thread as executed itself. After its execution, the resulting output is sent to the Event Queue. The Event Loop is responsible for sending functions' results to the Stack for processing when it becomes empty. Example: Let's understand it by using a simple example. JavaScript console.log("Hello"); setTimeout(console.log("Hello-2"), 3000); function sum(a, b) { console.log(a + b); } sum(2, 4); Output: Hello 6 Hello-2 Explanation: The execution flow starts from console.log("Hello"); and at the time it reaches the setTimeout function setTimeout(console.log("Hello-2"), 3000); It sends the function to the browser API where the function starts to execute, while in the meantime the next operation in the Stack is executed which is sum(2, 4) ; Then when the setTimeout function completed its execution it sends it to the Event Queue and waits for the completion of the running operations in the Stack, when the Stack finishes its operation, the Event Loop sends the setTimeout function from the Queue to the Stack, and Stack finally processed it. --Stack-- sum(2, 4) setTimeout(console.log("Hello-2"), 3000) console.log("Hello") Comment More infoAdvertise with us Next Article Explain Event-Driven Programming in Node.js D devaadi Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads What is Poll Phase in Node.js Event Loop ? The Node.js event loop is the heart of Node.js, responsible for executing non-blocking I/O operations and enabling the asynchronous behavior that defines the platform. Among the various phases of the event loop, the poll phase plays a crucial role in handling I/O events. In this article, we'll delve 5 min read Explain the Process Object in Node.js In this article, we are going to explore about process object of Node.js in detail. The Process object in node.js is a global object that can be used in any module without requiring it in the environment. This object is useful in the perspective of getting the information related to the node.js envi 3 min read Explain V8 engine in Node.js The V8 engine is one of the core components of Node.js, and understanding its role and how it works can significantly improve your understanding of how Node.js executes JavaScript code. In this article, we will discuss the V8 engineâs importance and its working in the context of Node.js.What is a V8 7 min read Explain the working of Node.js Welcome to the world of Node.js, an open-source runtime environment that has transformed the landscape of backend development. Traditionally, JavaScript was confined for frontend development, powering user interactions on the browser. However, with the advent of Node.js, JavaScript has broken free f 4 min read Explain Event-Driven Programming in Node.js Event-driven programming lies at the core of Node.js, defining its asynchronous nature and facilitating efficient handling of I/O operations. This article provides an in-depth explanation of event-driven programming in Node.js, its key concepts, and practical applications. Table of Content Understan 5 min read Non-Blocking event loop in Node.js Node.js operates on a single-threaded, event-driven architecture that relies heavily on non-blocking I/O operations to handle concurrent requests efficiently. This approach is enabled by its event loop mechanism, which allows Node.js to handle multiple requests concurrently without creating addition 5 min read Like