<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hammad Shah on Medium]]></title>
        <description><![CDATA[Stories by Hammad Shah on Medium]]></description>
        <link>https://medium.com/@mzgbfqs?source=rss-50c973bcd3a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Hammad Shah on Medium</title>
            <link>https://medium.com/@mzgbfqs?source=rss-50c973bcd3a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:55:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mzgbfqs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Event Loop, Call Stack, Event Queue, and Job Queue]]></title>
            <link>https://medium.com/@mzgbfqs/event-loop-call-stack-event-queue-and-job-queue-be355e4280c7?source=rss-50c973bcd3a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/be355e4280c7</guid>
            <dc:creator><![CDATA[Hammad Shah]]></dc:creator>
            <pubDate>Mon, 01 Jul 2024 08:38:08 GMT</pubDate>
            <atom:updated>2024-07-01T08:38:08.027Z</atom:updated>
            <content:encoded><![CDATA[<p>In JavaScript, understanding the Event Loop, Call Stack, Event Queue, and Job Queue is crucial to comprehend how asynchronous operations are handled. Let’s break down each component:</p><h3>1. Call Stack</h3><p>The call stack is a data structure that keeps track of function calls. It follows the Last In, First Out (LIFO) principle. When a function is called, it’s added to the top of the stack, and when it returns, it’s removed from the top.</p><h3>2. Event Loop</h3><p>The event loop is a mechanism that continuously checks the call stack and the task queues. It ensures that the call stack is empty before executing tasks from the queues.</p><h3>3. Event Queue (or Task Queue)</h3><p>The event queue holds messages (tasks) from various events (like click events, timer events, etc.) that need to be processed. When the call stack is empty, the event loop moves tasks from the event queue to the call stack for execution.</p><h3>4. Job Queue (or Microtask Queue)</h3><p>The job queue holds microtasks such as promises’ .then() handlers and MutationObserver callbacks. These tasks are executed before the event queue&#39;s tasks once the call stack is empty.</p><h3>Diagram</h3><p>Here’s a simplified diagram to visualize these components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*uWEkGq_r5ceBs2XuV6UhdQ.png" /></figure><h3>Explanation with Example</h3><p>Let’s use an example to explain these components in action:</p><pre>console.log(&#39;Start&#39;);<br><br>setTimeout(() =&gt; {<br>  console.log(&#39;Timeout&#39;);<br>}, 0);<br><br>Promise.resolve().then(() =&gt; {<br>  console.log(&#39;Promise&#39;);<br>});<br><br>console.log(&#39;End&#39;);</pre><p>Execution flow:</p><ol><li><strong>Call Stack</strong>: Starts with the global context and console.log(&#39;Start&#39;).</li><li><strong>Call Stack</strong>: console.log(&#39;Start&#39;) is executed, and &quot;Start&quot; is printed.</li><li><strong>Call Stack</strong>: setTimeout is called, and its callback is placed in the <strong>Event Queue</strong>.</li><li><strong>Call Stack</strong>: Promise.resolve().then is called, and its callback is placed in the <strong>Job Queue</strong>.</li><li><strong>Call Stack</strong>: console.log(&#39;End&#39;) is executed, and &quot;End&quot; is printed.</li><li><strong>Call Stack</strong>: The global context is cleared.</li></ol><p><strong>Event Loop</strong> checks:</p><ol><li><strong>Job Queue</strong>: The callback from the promise (console.log(&#39;Promise&#39;)) is moved to the <strong>Call Stack</strong> and executed. &quot;Promise&quot; is printed.</li><li><strong>Event Queue</strong>: The callback from setTimeout (console.log(&#39;Timeout&#39;)) is moved to the <strong>Call Stack</strong> and executed. &quot;Timeout&quot; is printed.</li></ol><h3>Final Output:</h3><pre>Start<br>End<br>Promise<br>Timeout</pre><p>This example shows how the Event Loop prioritizes the Job Queue over the Event Queue once the Call Stack is clear.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be355e4280c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>