<?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: Chaitanya Khachane</title>
    <description>The latest articles on DEV Community by Chaitanya Khachane (@chait04).</description>
    <link>https://dev.to/chait04</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%2F478595%2F4c691f89-e2b2-4554-9215-32325a7bc700.jpg</url>
      <title>DEV Community: Chaitanya Khachane</title>
      <link>https://dev.to/chait04</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chait04"/>
    <language>en</language>
    <item>
      <title>Execuion context, Event Loop, callback queue</title>
      <dc:creator>Chaitanya Khachane</dc:creator>
      <pubDate>Wed, 07 Jul 2021 16:49:36 +0000</pubDate>
      <link>https://dev.to/chait04/execuion-context-event-loop-callback-queue-2aec</link>
      <guid>https://dev.to/chait04/execuion-context-event-loop-callback-queue-2aec</guid>
      <description>&lt;h1&gt;
  
  
  Explained Async JS in 4min read. ( callback queue &amp;amp; Event Loop )
&lt;/h1&gt;

&lt;p&gt;Post is distributed in 3 parts-&amp;gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Execution Context&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;callback Queue&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Event Loop&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;hireMe&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`you are hired`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hireMe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Me First!`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(1) &lt;strong&gt;execution context&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As execution starts it will declare a placeholder for hireMe in a global execution context and will store the function definition in it...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As it reaches to setTimeout, it will create a timer in the browser with the given amount of time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remember as we invoke a function first it goes to call stack then its execution context is created and it gets destroyed as the function returns something.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The interesting things start from here&lt;/p&gt;

&lt;p&gt;(2). &lt;strong&gt;Callback queue - let's understand how it works&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;So what setTimeout will do is, it will add the hireMe function in the callback queue as the timer finishes, which is specifically made for async calls.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Note the function will not be added directly to the call stack as the timer finishes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;callback queue is specifically made for Asynchronous calls&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It doesn't matter what time you have specified in setTimeout&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(3). &lt;strong&gt;Event Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now comes the &lt;code&gt;Event Loop&lt;/code&gt;, which checks the call stack if it's empty or not. If nothing is inside the call stack then the function from the callback queue gets pushed in the call stack.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Event Loop&lt;/code&gt; is an infinite loop which is consistently checking the call stack if it is empty or not.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From what we have seen till We can say, functions that are in the &lt;code&gt;callback&lt;/code&gt; queue need to wait until the whole Global execution gets finished and whether the call stack is empty or not...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;so you can also have millions of &lt;code&gt;console.logs()&lt;/code&gt; in the global context and they all will run before the &lt;code&gt;hireMe()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is one more thing called Microtask queue i will explain it in next post ♥&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
