{"id":441,"date":"2025-05-09T11:39:17","date_gmt":"2025-05-09T11:39:17","guid":{"rendered":"https:\/\/scriptut.com\/?p=441"},"modified":"2025-05-09T11:39:18","modified_gmt":"2025-05-09T11:39:18","slug":"node-js-event-loop-overview","status":"publish","type":"post","link":"https:\/\/scriptut.com\/node-js\/node-js-event-loop-overview\/","title":{"rendered":"Node.js Event Loop Overview"},"content":{"rendered":"\n<p>The <strong>Event Loop<\/strong> in Node.js is a core part of its <strong>non-blocking, asynchronous<\/strong> architecture. It allows Node.js to handle many concurrent operations without creating a new thread for each request. This is possible because of its single-threaded nature combined with an event-driven, non-blocking I\/O model.<\/p>\n\n\n\n<p>How the Event Loop Works in Node.js<\/p>\n\n\n\n<p><strong>Timers Phase:<\/strong> Executes callbacks scheduled by <code>setTimeout()<\/code> and <code>setInterval()<\/code>.<\/p>\n\n\n\n<p><strong>Pending Callbacks Phase:<\/strong> Executes I\/O callbacks deferred by the operating system.<\/p>\n\n\n\n<p><strong>Idle, Prepare Phase:<\/strong> Internal operations for the event loop (mostly for Node.js internals).<\/p>\n\n\n\n<p><strong>Poll Phase:<\/strong> Retrieves new I\/O events, executes I\/O callbacks, and handles other low-level tasks.<\/p>\n\n\n\n<p><strong>Check Phase:<\/strong> Executes <code>setImmediate()<\/code> callbacks.<\/p>\n\n\n\n<p><strong>Close Callbacks Phase:<\/strong> Executes callbacks like <code>socket.on('close')<\/code><\/p>\n\n\n\n<p>Event Loop Example in Node.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\"1. Start\");\n\n\/\/ Set a timeout (Timers Phase)\nsetTimeout(() => {\n    console.log(\"4. Timeout (setTimeout)\");\n}, 0);\n\n\/\/ Set an immediate (Check Phase)\nsetImmediate(() => {\n    console.log(\"5. Immediate (setImmediate)\");\n});\n\n\/\/ Simulate I\/O operation (Poll Phase)\nrequire(\"fs\").readFile(__filename, () => {\n    console.log(\"6. I\/O callback (fs.readFile)\");\n\n    \/\/ Nested immediate to demonstrate event loop phases\n    setImmediate(() => {\n        console.log(\"7. Nested Immediate\");\n    });\n\n    \/\/ Nested timeout to demonstrate event loop phases\n    setTimeout(() => {\n        console.log(\"8. Nested Timeout\");\n    }, 0);\n});\n\nconsole.log(\"2. End\");\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Expected Output:<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start<\/li>\n\n\n\n<li>End<\/li>\n\n\n\n<li>Timeout (setTimeout)<\/li>\n\n\n\n<li>Immediate (setImmediate)<\/li>\n\n\n\n<li>I\/O callback (fs.readFile)<\/li>\n\n\n\n<li>Nested Immediate<\/li>\n\n\n\n<li>Nested Timeout<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<p><strong>Start and End:<\/strong> The synchronous code runs first.<\/p>\n\n\n\n<p><strong>Timers (<code>setTimeout<\/code>)<\/strong> are executed after the current operation completes.<\/p>\n\n\n\n<p><strong>Immediate (<code>setImmediate<\/code>)<\/strong> callbacks are placed in the check phase, which runs after the poll phase.<\/p>\n\n\n\n<p><strong>I\/O Callbacks<\/strong> are executed once the file is read, during the poll phase.<\/p>\n\n\n\n<p><strong>Nested Immediates<\/strong> are prioritized over nested timeouts within the same I\/O callback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Event Loop in Node.js is a core part of its non-blocking, asynchronous architecture. It allows Node.js to handle many concurrent operations without creating a new thread for each request. This is possible because of its single-threaded nature combined with &#8230; <a class=\"more-link\" href=\"https:\/\/scriptut.com\/node-js\/node-js-event-loop-overview\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,15],"tags":[],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-javascripts","category-node-js"],"_links":{"self":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/comments?post=441"}],"version-history":[{"count":1,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/posts\/441\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/media?parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/categories?post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptut.com\/wp-json\/wp\/v2\/tags?post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}