<?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: Papa Abdoulaye Diagne</title>
    <description>The latest articles on DEV Community by Papa Abdoulaye Diagne (@papidiagne30).</description>
    <link>https://dev.to/papidiagne30</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%2F345890%2F51547a9c-42f8-444d-99ab-4833326ad933.png</url>
      <title>DEV Community: Papa Abdoulaye Diagne</title>
      <link>https://dev.to/papidiagne30</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/papidiagne30"/>
    <language>en</language>
    <item>
      <title>Javascript Event Loop for dummies 🤡</title>
      <dc:creator>Papa Abdoulaye Diagne</dc:creator>
      <pubDate>Mon, 08 Feb 2021 22:03:25 +0000</pubDate>
      <link>https://dev.to/papidiagne30/javascript-event-loop-for-dummies-1bdi</link>
      <guid>https://dev.to/papidiagne30/javascript-event-loop-for-dummies-1bdi</guid>
      <description>&lt;p&gt;If someone who is not in the tech industry (a Muggle 😆) asks you what JavaScript is, could you define it? Well, you could say something like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Javascript is an &lt;strong&gt;asynchronous&lt;/strong&gt;, &lt;strong&gt;single-threaded&lt;/strong&gt; and &lt;strong&gt;non-blocking&lt;/strong&gt; language which uses some tools like a &lt;strong&gt;call stack&lt;/strong&gt;, an &lt;strong&gt;event loop&lt;/strong&gt;, a &lt;strong&gt;callback queue&lt;/strong&gt; and some other &lt;strong&gt;APIs&lt;/strong&gt; for &lt;strong&gt;concurrency&lt;/strong&gt;".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But BRUUUH??!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/toB3AnUDkqE3GENKx0/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/toB3AnUDkqE3GENKx0/giphy.gif" alt="Confused GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That sounds like a dangerous spell in Harry Potter that would send your enemy straight to hell, doesn't it? But don't worry, I don't like when people use some complicated words to say barely nothing either. So let's go through each of those bizarre words.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Call stack&lt;/strong&gt;: that is where our tasks (functions encountered in the code) are stacked. When we encounter a function, we push it to the stack and when we return from that function, we pop it off the stack (LIFO)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single-threaded&lt;/strong&gt;: in simple words, JavaScript can only do one thing at a time (one call stack). One element (function) in the stack is executed at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-blocking&lt;/strong&gt;: it means that whenever a "slow" task (I'll tell you what i mean by that later) is encountered in the call stack, it doesn't block the execution of the following tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Asynchronous&lt;/strong&gt;: The non-blocking behavior we explained above is done by using &lt;strong&gt;asynchronous callbacks&lt;/strong&gt; which means that we run the "slow" task, give it a callback (an action to do when it's done), and run that callback later.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Callback queue&lt;/strong&gt;: basically the place where the callbacks are queued (FIFO) waiting for their turn to be called by our MVP, the call stack 😎&lt;/li&gt;
&lt;li&gt;The famous &lt;strong&gt;Event Loop&lt;/strong&gt;: that's what this whole article is about. Basically, it's the process of  checking if the call stack is empty, and if so, taking off the first element (the first callback) in the queue (if it is not empty, of course 🙄) and pushing it to the stack for it to be executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Easy Peasy, isn't it 😌?! NO?! Alright, I admit, that's a lot of words. So let's see a straightforward example to explain all of this.&lt;/p&gt;




&lt;p&gt;Let's admit we're in a restaurant where some &lt;strong&gt;orders&lt;/strong&gt; are already cooked and are quick to serve by the &lt;strong&gt;waitress&lt;/strong&gt;, and some other are harder to make and are going to take time to be cooked by the &lt;strong&gt;chef&lt;/strong&gt; (the "slow" tasks I was talking about 😉). We imagine that whatever the chef cooks, he puts it on his big table for the waitress to take it (It's my article, I do what i want, okay? 😒)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbcyir11p5bqkal01pdld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbcyir11p5bqkal01pdld.png" alt="Restaurant example image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we suppose that our waitress has 3 orders on her list. The quick orders are represented in white and the "slow" ones are represented in blue. They are &lt;strong&gt;stacked&lt;/strong&gt; in her list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsxceheoh9t2y18pssw8n.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsxceheoh9t2y18pssw8n.gif" alt="Stacked functions GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The order on the top of the list (a hamburger) is then served to client 4 and get's removed from the list when it's done &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmue80zgnyai9n968vaof.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmue80zgnyai9n968vaof.gif" alt="Get hamburger GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can notice that the next order is a slow order. So the waitress gives it to the chef who cooks it, and puts the bowl of cooked salad on his table, for the waitress to serve it later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5rtbyez3k7trxwrjipwb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5rtbyez3k7trxwrjipwb.gif" alt="Get salad GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, the waitress takes care of the next order (a cup of juice) which is quick, and serves it to the client 4 (he's quite hungry 😅)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpl14qjezikwdwincroyt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpl14qjezikwdwincroyt.gif" alt="Get juice GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it's done, the waitress notices that her list is empty. So she checks the chef's table to see if there is any meal on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa1vqlespaz0fri7541nx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa1vqlespaz0fri7541nx.gif" alt="Check table GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If so, she takes that meal, and serves it the corresponding client (in this case, she serves a delicious bowl of salad the client 3)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F68zqxk6aoi9pq2kht7fc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F68zqxk6aoi9pq2kht7fc.gif" alt="Salad Bowl GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Her list being still empty, she checks the chef's table once again and see's that there's nothing on it. Thus, she can clock out and go home 🎉.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1xijvge18o933rzlkeiy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1xijvge18o933rzlkeiy.gif" alt="Second table check GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it!! That's how JavaScript works. You still can't make the connection between the example and the weird words above 😩? Alright, let's see, with the same animation, how the following code is going to be executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;()&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Slow!!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;7000&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bye!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First of all, let's place our weird words to where they belong in the animation&lt;/p&gt;

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

&lt;p&gt;Now let's see how it works&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmo2h5r746hsicv0jjeht.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmo2h5r746hsicv0jjeht.gif" alt="JS Process GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;main()&lt;/code&gt; function specified here is just the main thread of our code.&lt;/li&gt;
&lt;li&gt;Each function is pushed to the stack when it is called, and popped off it when it returns something.&lt;/li&gt;
&lt;li&gt;Our slow function (&lt;code&gt;setTimeout()&lt;/code&gt;) is not handled by the javascript engine, but by some WebAPIs in the browser which will push the resulting callback into the queue when it's done.&lt;/li&gt;
&lt;li&gt;Meanwhile, the functions in the stack continue to get executed. Whenever the stack is empty, the event loop checks if the callback queue is empty, takes the first element in it (our callback function) and push it to the call stack&lt;/li&gt;
&lt;li&gt;The execution continues until there's nothing left in the call stack or in the callback queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See? It's not that complicated, right? &lt;/p&gt;




&lt;p&gt;I hope this article helped you understand better the famous event loop and made you feel better with javascript. It is fundamental to understand these concepts in order to avoid some strange behaviors when you write asynchronous functions. If it still seems confusing to you, I recommend &lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;ab_channel=JSConf" rel="noopener noreferrer"&gt;this excellent video of Philip Roberts&lt;/a&gt; explaining it:&lt;/p&gt;

&lt;p&gt;Feel free to reach out to me if you have any questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Github: &lt;a href="https://github.com/nushen96" rel="noopener noreferrer"&gt;nushen96&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Twitter: &lt;a href="https://twitter.com/papidiagne30" rel="noopener noreferrer"&gt;papidiagne30&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Peace out!! ✌🏾&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>async</category>
      <category>promises</category>
    </item>
  </channel>
</rss>
