<?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: Tashfeen Rao</title>
    <description>The latest articles on DEV Community by Tashfeen Rao (@tashfeenrao).</description>
    <link>https://dev.to/tashfeenrao</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%2F393040%2F08661488-a7bf-4dbe-b7b3-9e76c4feb6c4.jpg</url>
      <title>DEV Community: Tashfeen Rao</title>
      <link>https://dev.to/tashfeenrao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tashfeenrao"/>
    <language>en</language>
    <item>
      <title>Hard Parts of Asynchronous Javascript</title>
      <dc:creator>Tashfeen Rao</dc:creator>
      <pubDate>Fri, 05 Jun 2020 23:09:54 +0000</pubDate>
      <link>https://dev.to/tashfeenrao/hard-parts-of-asynchronous-javascript-24hf</link>
      <guid>https://dev.to/tashfeenrao/hard-parts-of-asynchronous-javascript-24hf</guid>
      <description>&lt;p&gt;Javascript engine runs code line by line which we call synchronous behavior, there's another type of execution that javascript engine does is known as &lt;code&gt;asynchronous javascript&lt;/code&gt;. Before jumping into the beautiful world of &lt;code&gt;asynchronous&lt;/code&gt; we must understand why we require this type of execution.&lt;/p&gt;

&lt;p&gt;when you write code sometimes there is a function whose input depends on another function's output. but if other function takes time to give you the result then what will you do in meantime except waiting which is a very bad practice. In the case of the web when we &lt;code&gt;call API&lt;/code&gt;  for data it usually takes time to return it is a very frustrating experience for the user if he had to wait until the response comes and do other stuff that can be done in meantime.&lt;/p&gt;

&lt;p&gt;A real example for this in your Pc you can open multiple programs and your system don't mind because it has multiple processors it shifts load among them and how is it done? well, you can imagine, it is asynchronous which allows us to run the program in the background.&lt;/p&gt;

&lt;p&gt;For understanding the &lt;code&gt;asynchronously javascript.&lt;/code&gt; we need to make a &lt;code&gt;mental model&lt;/code&gt; in our head to understand what's going on behind the scenes and how the &lt;code&gt;javascript engine&lt;/code&gt; executes our code.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;ES6&lt;/code&gt; &lt;code&gt;async functions&lt;/code&gt; were introduced for this purpose. let's start with a very basic &lt;code&gt;async function&lt;/code&gt; example for making our mental model.&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createFlow&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="s1"&gt;Me first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://twitter.com/tashfeen/tweets/1&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;createFlow&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="s1"&gt;Me second&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;Do you have any idea what will be print on the screen? well, let's figure out the answer. First, we want to visualize how this code will execute on the javascript engine.&lt;br&gt;
Javascript engine consists of three main elements&lt;br&gt;
1 Execution context &lt;br&gt;
2 Memory&lt;br&gt;
3 call-stack&lt;br&gt;
The execution context runs the code and displays it on screen. Memory stores variables functions etc. call-stack runs functions in first in last out principle. At the bottom, there is another type call queue-stack that holds those functions that wait for some browser work to finish.&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%2Fi72esuzd6mw0yxaudxw7.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%2Fi72esuzd6mw0yxaudxw7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you get your visualization. let's see how it gonna be executed our code.&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createFlow&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="s1"&gt;Me first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://twitter.com/tashfeen/tweets/1&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the first line of code, we have a function expression. It will store function definition at &lt;code&gt;Memory&lt;/code&gt; and goes to the next line. &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%2Ff686as5sdq8jkpvi37dy.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%2Ff686as5sdq8jkpvi37dy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next line is invoking createFlow() function.&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="nf"&gt;createFlow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript engine first looks into the &lt;code&gt;global Memory&lt;/code&gt; is there any function with the name of creatFlow()? yes, it found one then it will put this function inside call-stack and It will create its own execution inside of the global Execution context. Now it start executing function's code line by line.&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="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="s1"&gt;Me first&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;this will be print on the screen. then it goes to the second line which's a variable definition.&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;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://twitter.com/tashfeen/tweets/1&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;It will store the &lt;code&gt;data&lt;/code&gt; variable in &lt;code&gt;Memory.&lt;/code&gt; Its value is blank in &lt;code&gt;Memory&lt;/code&gt; right now. The right side of this variable invokes browser's facade function &lt;code&gt;fetch()&lt;/code&gt; which triggers web browser feature work to get the data from the twitter server. &lt;code&gt;fetch()&lt;/code&gt; will return promise object which has two things &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;onfulfillment[]&lt;/code&gt;. When the response comes from the server it fills the &lt;code&gt;value&lt;/code&gt;. If there is some work to be done on this &lt;code&gt;value&lt;/code&gt; it will be done &lt;code&gt;onfulfillment[].&lt;/code&gt; For visualizing this process, I made a diagram.&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%2Fobd56fb4bj8p30vnzwyy.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%2Fobd56fb4bj8p30vnzwyy.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Browser background work takes time to get back with data. How we will &lt;code&gt;console.log(data)&lt;/code&gt; when we don't have data? Are we going to sit idle and wait for a response?  You are right, the answer is NO. But how we execute the rest of the code. Well In the above code you saw special browser feature &lt;code&gt;await&lt;/code&gt; which will through us out of function's execution context, and put creatFlow function in queue-stack. Now it came to global execution context and execute next line in code&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="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="s1"&gt;Me second&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;It'll print &lt;code&gt;Me second&lt;/code&gt; on screen. Now there's no other code left for execution.&lt;/p&gt;

&lt;p&gt;I am glad that you asked what happened to our&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="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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How will we go back inside of the &lt;code&gt;createFlow()&lt;/code&gt; execution context?.  Well, when we get a response from the &lt;code&gt;twitter server&lt;/code&gt; It'll fill the &lt;code&gt;value&lt;/code&gt; property of &lt;code&gt;promise&lt;/code&gt; object and put &lt;code&gt;createFlow()&lt;/code&gt; on call-stack and start executing where it left earlier. which is&lt;br&gt;
We got our data from the &lt;code&gt;twitter server.&lt;/code&gt; which is a string with simple &lt;code&gt;HI&lt;/code&gt; It fills &lt;code&gt;value&lt;/code&gt; property of &lt;code&gt;promise&lt;/code&gt; object and stores this &lt;code&gt;value = 'HI'&lt;/code&gt; in &lt;code&gt;Memory&lt;/code&gt; of function's execution context.Now Javascript reads&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="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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Javascript looks for &lt;code&gt;data&lt;/code&gt; variable into the &lt;code&gt;Memory&lt;/code&gt; and found with &lt;code&gt;data = 'HI'&lt;/code&gt; Which will be print out on screen after about &lt;code&gt;200ms&lt;/code&gt;.&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%2Fz24jxyr5egtea93ernol.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%2Fz24jxyr5egtea93ernol.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is asynchronous we left some code for running in background which requires some time to finish. We get back to other code and start executing it. When we got our response we execute left code. Now the execution cycle is complete.&lt;/p&gt;

</description>
      <category>fetch</category>
      <category>await</category>
      <category>asynchronous</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
