

{"id":102270,"date":"2021-11-02T09:00:53","date_gmt":"2021-11-02T03:30:53","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=102270"},"modified":"2021-11-02T13:36:10","modified_gmt":"2021-11-02T08:06:10","slug":"nodejs-event-loop-and-event-emitter","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/","title":{"rendered":"Nodejs Event Loop and Event Emitter"},"content":{"rendered":"<p>In this article, we will be discussing what is event loop in Nodejs, what are the different phases, what is event emitter, how it works and what are the different methods present in it.<\/p>\n<h3>Difference between Nodejs Events and callback:<\/h3>\n<p>In callback, the function executes after the asynchronous function has returned its result whereas a function in event executes as soon as the corresponding event has been fired.<\/p>\n<h3>Event Loop in Nodejs:<\/h3>\n<p>Node js uses javascript and we know that javascript is single threaded and synchronous than how node js maintains concurrency? In order to achieve it, nodejs has events and callbacks. Event loop is an infinite loop and in each iteration it checks the event queue for any triggered event, if found it then executes it and removes it from the queue.<\/p>\n<h3>Phases in Event Loop:<\/h3>\n<h4>1. Timers:<\/h4>\n<p>In this phase, execution of callbacks scheduled by setTimeout() and setInterval() takes place.<\/p>\n<p>The callbacks made by setTimeout() and setInterval() are known as timer callbacks, and these callbacks will try to execute as soon as the specified time has passed.<\/p>\n<h4>2. Pending Callbacks:<\/h4>\n<p>In this phase, execution of callbacks that were deferred takes place.<\/p>\n<h4>3. Poll:<\/h4>\n<p>It retrieves new I\/O events and executes I\/O callbacks<\/p>\n<h4>4. Check:<\/h4>\n<p>Callbacks from setImmediate() are invoked here.<\/p>\n<h4>5. Close callbacks:<\/h4>\n<p>Some of the close callbacks are socket.on(\u2018close\u2019) etc<\/p>\n<h3>Nodejs setImmediate() vs setTimeout()<\/h3>\n<p><strong>setImmediate():<\/strong> It executes once the current poll phase is over<\/p>\n<p><strong>setTimeout():<\/strong> It executes after a threshold amount of time has passed.<\/p>\n<p>The order of execution of the above timer depends where we are using them, if both are written in the main module then it can be impacted by other applications.<\/p>\n<p><strong>Example of setimmediate and setTimeout in Nodejs:<\/strong><\/p>\n<p>When you run the below code you will observe that sometimes setTimeout() executes first and sometimes setImmediate() executes first.<\/p>\n<p><strong>Code for comparing setimmediate and setTimeout:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">setTimeout(() =&gt; {\r\n    console.log('setTimeout executed');\r\n}, 0);\r\n \r\nsetImmediate(() =&gt; {\r\n    console.log('setImmediate executed');\r\n});\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-setimmediate-and-set-timeout-in-main.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103743\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-setimmediate-and-set-timeout-in-main.webp\" alt=\"nodejs-setimmediate and set timeout in main\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-setimmediate-and-set-timeout-in-main.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-setimmediate-and-set-timeout-in-main-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>When we will be using these timers inside an I\/O cycle then always the setImmediate will execute first.<\/p>\n<p><strong>Code for setimmediate and setTimeout inside filesystem<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const fs = require('fs');\r\n \r\nfs.readFile(\"DataFlair.txt\", () =&gt; {\r\n    setTimeout(() =&gt; {\r\n        console.log('timeout');\r\n    }, 0);\r\n    setImmediate(() =&gt; {\r\n        console.log('immediate');\r\n    });\r\n});<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-settimout-and-set-immedaite-inside-IO.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103744\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-settimout-and-set-immedaite-inside-IO.webp\" alt=\"nodejs settimout and set immedaite inside IO\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-settimout-and-set-immedaite-inside-IO.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-settimout-and-set-immedaite-inside-IO-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>process.nextTick():<\/h3>\n<p>It is a part of asynchronous api. Whenever we pass callbacks to process.nextTick() what happens is that all these callbacks are resolved before the continuation of the event loop. Due to this, starvation may occur.<\/p>\n<h3>Why to use process.nextTick()?<\/h3>\n<p>It allows users to easily handle errors, cleanups and also if we want to run a callback after the call stack was unwound.<\/p>\n<h3>process.nextTick() vs setImmediate() in Nodejs:<\/h3>\n<p>The callbacks in process.nextTick() are called immediately whereas in setImmediate() callbacks execute in the next iterations of the event loop.<\/p>\n<h3>Nodejs Event Emitter and its working<\/h3>\n<p>Event Emitter is a class that is capable of emitting an event, attaching an event and also detaching it. Https and other node js core modules use the event emitter class.<\/p>\n<p>Event emitter class calls all the events in the same order in which they were registered.<\/p>\n<p>The on() method is used to attach the event handler, this method takes a callback function which gets attached to the event. More than one function can also be attached to a single event.<\/p>\n<p>The emit() method is used to emit the attached event, basically it invokes the attached function.<\/p>\n<p><strong>Code for event emitter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1() {\r\n    console.log('First event occurred!')\r\n}\r\n \r\nfunction event2() {\r\n    console.log('First event occurred!')\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.on('someevent', event2)\r\nemitter.emit('someevent')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-emit-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103745\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-emit-method.webp\" alt=\"node.js emit method\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-emit-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-emit-method-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>In the above image, you can see that the function event1 and event 2 has been registered by the name \u201csomeevent\u201d using the on() method.<\/p>\n<p>Next we used the emit method to fire the event and we can see the output of both the functions which means that both the functions were invoked as soon as the \u201csomeevent\u201d event was fired.<\/p>\n<h3>Emitting event with parameter.<\/h3>\n<p>We can also pass data to the call back function. In order to do so we have to pass the data in the second argument of emit() method.<\/p>\n<p><strong>Code for event emitter with parameter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-pararmeter-emit-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103746\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-pararmeter-emit-method.webp\" alt=\"nodejs pararmeter emit method\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-pararmeter-emit-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-pararmeter-emit-method-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that we have passed a name with value \u201cDataFlair\u201d in our second argument.This value is then used by the event1 function.<\/p>\n<h3>Event Emitter Methods:<\/h3>\n<p>Below are the methods present in the event emitter with explanation and code.<\/p>\n<h3>Detach Event:<\/h3>\n<p>We can detach the previously attached event by using the off() method.<\/p>\n<p><strong>Code for detaching event:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-detach-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103747\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-detach-method.webp\" alt=\"node.js detach method\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-detach-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-detach-method-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that we have used emit method twice to emit the same event but the output came only for that emit which was called before detaching the event using the off() method.<\/p>\n<p>So once you detach an event then the emit method does not have any effect for that event.<\/p>\n<h3>Nodejs Count Listener:<\/h3>\n<p>listenerCount() method returns the number of listeners for that event. You can see in the below image that listenerCount() return 1 at first than as we called the off() method the listener for the event is detached and hence the count of listener is now zero.<\/p>\n<p><strong>Code for count listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.listenerCount('someevent'))\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.listenerCount('someevent'))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-listener-count.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103748\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-listener-count.webp\" alt=\"node.js listener count\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-listener-count.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-listener-count-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Nodejs Raw Listener:<\/h3>\n<p>It returns an array of listeners for that particular event.<\/p>\n<p><strong>Code for raw listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.rawListeners('someevent'))\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.rawListeners('someevent'))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-raw-listener.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103749\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-raw-listener.webp\" alt=\"node.js raw listener\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-raw-listener.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-raw-listener-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Before calling the off() method there was one listener so it returned its type and name.<\/p>\n<p>After the off() method was called nothing was attached to the event so it returned an empty array.<\/p>\n<h3>Nodejs Add Listener:<\/h3>\n<p>This is similar to the on method. It takes the event name and a callback function and then attaches the event. It checks the event array to see if the event has already been attached or not. If the event is attached it returns the array, else return empty array.<\/p>\n<p><strong>Code for adding listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.addListener('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-addlistener.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103750\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-addlistener.webp\" alt=\"node.js addlistener\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-addlistener.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-addlistener-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that the result is same as the on() method<\/p>\n<h3>Remove Listener:<\/h3>\n<p>It is similar to the off() method. It checks in the event array for the event and it removes it from the event array.<\/p>\n<p><strong>Code for removing listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.addListener('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nemitter.removeListener('someevent', event1);\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-removelistener.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103751\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-removelistener.webp\" alt=\"node.js removelistener\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-removelistener.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-removelistener-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>The answer is the same as that for the off() method.<\/p>\n<h3>Once() method in Nodejs:<\/h3>\n<p>If we want an event to be used only one time then we can use the once() method. It takes eventname and a callback function.<\/p>\n<p>When the event is first emitted it invokes the callback function and after that it detaches the event automatically, after that it will not respond to any more emitted calls.<\/p>\n<p><strong>Code for once():<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\nfunction event1() {\r\n    console.log('Welcome to DataFlair');\r\n}\r\nemitter.once('someevent', event1)\r\nemitter.emit('someevent')\r\nemitter.emit('someevent')\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-once-emiiter.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103752\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-once-emiiter.webp\" alt=\"node.js once emitter\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-once-emiiter.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/node.js-once-emiiter-768x408.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that the once method invoked the callback function only when the first emit() method executed, the next emit method did not invoke the callback function as the event already got detached.<\/p>\n<h3>Summary<\/h3>\n<p>In this article we have seen the event loop of node js and built in methods of event emitter. We hope you enjoyed the article. Do check out DataFlair\u2019s other blogs too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be discussing what is event loop in Nodejs, what are the different phases, what is event emitter, how it works and what are the different methods present in it.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":103990,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25271,25657,25658,25659],"class_list":["post-102270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-event-emitter","tag-nodejs-event-loop","tag-phases-in-nodejs-event-loop","tag-working-of-event-emitter-in-nodejs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Event Loop and Event Emitter - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is Nodejs event loop, different phases in event loop, event emitter, how it works and different methods present in it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Event Loop and Event Emitter - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is Nodejs event loop, different phases in event loop, event emitter, how it works and different methods present in it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-02T03:30:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-02T08:06:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nodejs Event Loop and Event Emitter - DataFlair","description":"Learn what is Nodejs event loop, different phases in event loop, event emitter, how it works and different methods present in it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Event Loop and Event Emitter - DataFlair","og_description":"Learn what is Nodejs event loop, different phases in event loop, event emitter, how it works and different methods present in it.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-11-02T03:30:53+00:00","article_modified_time":"2021-11-02T08:06:10+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Event Loop and Event Emitter","datePublished":"2021-11-02T03:30:53+00:00","dateModified":"2021-11-02T08:06:10+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/"},"wordCount":1103,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp","keywords":["Nodejs Event Emitter","Nodejs Event Loop","Phases in Nodejs Event Loop","working of event emitter in nodejs"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/","name":"Nodejs Event Loop and Event Emitter - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp","datePublished":"2021-11-02T03:30:53+00:00","dateModified":"2021-11-02T08:06:10+00:00","description":"Learn what is Nodejs event loop, different phases in event loop, event emitter, how it works and different methods present in it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/nodejs-event-loop-and-event-emitter.webp","width":1200,"height":628,"caption":"nodejs event loop and event emitter"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-loop-and-event-emitter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Event Loop and Event Emitter"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=102270"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102270\/revisions"}],"predecessor-version":[{"id":103739,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/102270\/revisions\/103739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/103990"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=102270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=102270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=102270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}