<?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: Zod</title>
    <description>The latest articles on DEV Community by Zod (@zod_the_god).</description>
    <link>https://dev.to/zod_the_god</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%2F2742712%2F86278054-33b8-4748-ad0c-a709817f47a2.png</url>
      <title>DEV Community: Zod</title>
      <link>https://dev.to/zod_the_god</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zod_the_god"/>
    <language>en</language>
    <item>
      <title>Nodejs Event loop</title>
      <dc:creator>Zod</dc:creator>
      <pubDate>Fri, 24 Jan 2025 15:47:19 +0000</pubDate>
      <link>https://dev.to/zod_the_god/nodejs-event-loop-5ab6</link>
      <guid>https://dev.to/zod_the_god/nodejs-event-loop-5ab6</guid>
      <description>&lt;p&gt;You might have heard that Nodejs is single threaded, but how is it able to handle multiple tasks concurrently ?&lt;/p&gt;

&lt;p&gt;All thanks to &lt;strong&gt;Event loop&lt;/strong&gt;. Let's try to understand how event loops helps in execution of synchronous and asynchronous code in Nodejs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xs0sub58y08212uhasj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xs0sub58y08212uhasj.png" alt="Event loop" width="800" height="724"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's a loop that is alive till our Nodejs application is up and running. In every iteration of event loop we have to go through 6 different queues, which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Timer Queue&lt;/strong&gt;: Handles setTimeout and setInterval callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I/O Queue&lt;/strong&gt;: Manages callbacks for I/O operations such as reading files and network requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check Queue&lt;/strong&gt;: Executes callbacks from setImmediate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close Queue&lt;/strong&gt;: Handles cleanup operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microtask Queue&lt;/strong&gt;: Includes two sub-queues:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Next Tick Queue&lt;/strong&gt;: Handles process.nextTick callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Promise Queue&lt;/strong&gt;: Handles resolved promises.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  So how does the event loop actually works ?
&lt;/h2&gt;

&lt;p&gt;Here is the execution order of event loop&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Executes all synchronous JavaScript code (highest priority).&lt;/li&gt;
&lt;li&gt;Process any pending task within microtask queue, first process.nextTick and then promise queue&lt;/li&gt;
&lt;li&gt;Execute callbacks in the timer queue.&lt;/li&gt;
&lt;li&gt;Check the microtask queue again.&lt;/li&gt;
&lt;li&gt;Execute callbacks in the I/O queue.&lt;/li&gt;
&lt;li&gt;Check the microtask queue again.&lt;/li&gt;
&lt;li&gt;Execute callbacks in the check queue.&lt;/li&gt;
&lt;li&gt;Check the microtask queue again.&lt;/li&gt;
&lt;li&gt;Execute callbacks in the close queue.&lt;/li&gt;
&lt;li&gt;Finally, process any remaining microtasks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After a complete cycle if any callbacks are still pending, the loops continues for one more iteration following the same path otherwise terminates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's take an example to make it more clear
&lt;/h2&gt;

&lt;p&gt;Here we have 3 console logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; console.log('one');
&amp;gt; console.log('two');
&amp;gt; console.log('three');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so what do you think about the output ?&lt;br&gt;
yes, you are right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; one
&amp;gt; two
&amp;gt; three
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let again take 3 console logs but let's put the second one inside  &lt;strong&gt;setTimeout&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; console.log('one');
&amp;gt; setTimeout(() =&amp;gt; {
&amp;gt;     console.log('two');
&amp;gt; }, 0);
&amp;gt; console.log('three');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so now what do you think about the output ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; one
&amp;gt; three
&amp;gt; two
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you guessed the same congratulations you are right again.&lt;br&gt;
so why this and why not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; one
&amp;gt; two
&amp;gt; three
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we have set &lt;code&gt;0 seconds&lt;/code&gt; as timeout !!&lt;/p&gt;

&lt;p&gt;As we mentioned earlier, all synchronous JavaScript code has the highest priority. As the &lt;code&gt;console log&lt;/code&gt; with &lt;code&gt;one &amp;amp; three&lt;/code&gt; are synchronous they are executed first. When the program execution reaches to line 2 it sees &lt;strong&gt;setTimeout&lt;/strong&gt; which is an asynchronous so pushes it to &lt;code&gt;timer queue&lt;/code&gt; and only after all synchronous task are completed the event loops comes into action and it sees that there exists a callback in &lt;code&gt;timer queue&lt;/code&gt; which is a &lt;code&gt;console log&lt;/code&gt; i.e. result of &lt;code&gt;setTimeout&lt;/code&gt;. It print the value &lt;code&gt;two&lt;/code&gt; on console and moves on. If there were other callbacks in other queues, they have to wait until their turn and then be executed. After first iteration as we have no other callbacks left then the event loop exists indicating the end of our program.&lt;/p&gt;

&lt;p&gt;So, That's all i know about event loop. If i've missed any points feel free to drop your comments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>eventloop</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
