<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kanha Kumar Khatua</title>
    <description>The latest articles on DEV Community by Kanha Kumar Khatua (@kanhakumar143).</description>
    <link>https://dev.to/kanhakumar143</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1033829%2Ff3506bc6-1a1f-4c67-b5b4-a7ffac944de3.jpeg</url>
      <title>DEV Community: Kanha Kumar Khatua</title>
      <link>https://dev.to/kanhakumar143</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kanhakumar143"/>
    <language>en</language>
    <item>
      <title>Event Loop javascript</title>
      <dc:creator>Kanha Kumar Khatua</dc:creator>
      <pubDate>Wed, 15 May 2024 17:32:37 +0000</pubDate>
      <link>https://dev.to/kanhakumar143/event-loop-javascript-238d</link>
      <guid>https://dev.to/kanhakumar143/event-loop-javascript-238d</guid>
      <description>&lt;h3&gt;
  
  
  Event Loop:-
&lt;/h3&gt;

&lt;p&gt;The event loop is a mechanism in JavaScript that allows the execution of &lt;strong&gt;asynchronous code.&lt;/strong&gt; It is a &lt;strong&gt;single-threaded&lt;/strong&gt; loop that &lt;em&gt;&lt;strong&gt;constantly checks for events&lt;/strong&gt;&lt;/em&gt; and then executes the appropriate callback functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting:-
&lt;/h3&gt;

&lt;p&gt;Hoisting refers to the process where the interpreter appears to move the declaration of function, variables, or classes to the top of their scope, period, and execution of the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F27lfxj8cuq2t8r6zgz7u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F27lfxj8cuq2t8r6zgz7u.png" alt="Image description" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When JS code runs it creates a global execution context and pushes it into the call stack and after the complete execution of the code it pops out from the call stack.&lt;/li&gt;
&lt;li&gt;If any WEB APIs come then it registers the timer web API or Browser after the timer is expired it pushes into the micro-task queue(high priority, Promises, async/await) or call-back queue (setTimeout, setInterval).&lt;/li&gt;
&lt;li&gt;Event loop watch the call stack if the call stack is empty then it pushes the task from the micro-task queue or call-back/task queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Starvation:-
&lt;/h3&gt;

&lt;p&gt;It happens because in the micro-task queue if one executes and creates another call-back function it continuously happens and the micro-task is not free due to this higher priority, it runs again and again whenever the call stack is free and there is no chance of the call-back queue to run, due to this call-back queue going into starvation.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is event loop in nodejs ?</title>
      <dc:creator>Kanha Kumar Khatua</dc:creator>
      <pubDate>Tue, 01 Aug 2023 10:59:32 +0000</pubDate>
      <link>https://dev.to/kanhakumar143/what-is-event-loop-in-nodejs--478l</link>
      <guid>https://dev.to/kanhakumar143/what-is-event-loop-in-nodejs--478l</guid>
      <description>&lt;p&gt;In Node.js, the event loop is a crucial part of its architecture that allows it to handle asynchronous operations efficiently. Understanding the event loop is essential for writing scalable and non-blocking applications in Node.js. Let's break down how the event loop works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Single-Threaded Non-Blocking Model:&lt;/strong&gt;&lt;br&gt;
Node.js operates on a single-threaded event loop model. Unlike traditional multi-threaded approaches, Node.js uses a single thread to handle all incoming requests and operations. This single thread does not get blocked by I/O operations, making it highly efficient in handling concurrent tasks.&lt;br&gt;
&lt;strong&gt;2. Event Loop Phases:&lt;/strong&gt;&lt;br&gt;
The event loop in Node.js consists of multiple phases, each serving a specific purpose. These phases are executed in a loop, and during each iteration, the event loop performs the following tasks:&lt;/p&gt;

&lt;p&gt;a. Timers: This phase executes callbacks scheduled by setTimeout() and setInterval() functions.&lt;/p&gt;

&lt;p&gt;b. Pending I/O callbacks: Executes I/O related callbacks (e.g., networking, file system operations) that were deferred during a previous loop iteration.&lt;/p&gt;

&lt;p&gt;c. Idle, prepare: These are internal phases, and they are generally not something developers need to be concerned about.&lt;/p&gt;

&lt;p&gt;d. Poll: This is the most critical phase where I/O operations are processed. Node.js checks for new I/O events, such as incoming connections or data being available for reading. If there are pending I/O operations, they are executed in this phase. If there are no pending I/O operations, Node.js will wait here.&lt;/p&gt;

&lt;p&gt;e. Check: Executes callbacks registered via setImmediate().&lt;/p&gt;

&lt;p&gt;f. Close callbacks: Executes cleanup callbacks, such as socket.on('close', ...).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Event Loop Flow:&lt;/strong&gt;&lt;br&gt;
The event loop operates in a continuous loop, following these steps:&lt;/p&gt;

&lt;p&gt;Executes any callbacks scheduled by setTimeout() and setInterval() that have met their timer criteria.&lt;/p&gt;

&lt;p&gt;Checks for I/O events (networking, file system, etc.) and executes their callbacks.&lt;/p&gt;

&lt;p&gt;Executes setImmediate() callbacks.&lt;/p&gt;

&lt;p&gt;If there are no more events to process, Node.js waits in the "Poll" phase for new events.&lt;/p&gt;

&lt;p&gt;After the "Poll" phase, Node.js performs some additional checks before starting the next loop iteration.&lt;/p&gt;

&lt;p&gt;The process repeats, ensuring that the event loop keeps handling new incoming events without blocking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Callbacks Execution:&lt;/strong&gt;&lt;br&gt;
Callbacks are functions that are registered to be executed when a particular event occurs. For example, when a network request is completed, the corresponding callback is triggered, allowing the application to respond to that event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Async and Non-Blocking:&lt;/strong&gt;&lt;br&gt;
Node.js is designed to perform I/O operations asynchronously and in a non-blocking manner. When an I/O operation is initiated (e.g., reading from a file, making a network request), Node.js does not wait for the result. Instead, it continues processing other tasks and registers a callback to be executed once the I/O operation is completed.&lt;/p&gt;

&lt;p&gt;This event loop architecture enables Node.js to handle a large number of concurrent connections efficiently. Developers can write code that maximizes the use of asynchronous functions and callbacks to prevent blocking operations and take advantage of Node.js's non-blocking capabilities.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>express</category>
      <category>mern</category>
    </item>
  </channel>
</rss>
