<?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: Bhavesh Kasturi</title>
    <description>The latest articles on DEV Community by Bhavesh Kasturi (@duhbhavesh).</description>
    <link>https://dev.to/duhbhavesh</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%2F386563%2Fad2d4d86-faf9-450b-bf58-86a5733b5699.jpg</url>
      <title>DEV Community: Bhavesh Kasturi</title>
      <link>https://dev.to/duhbhavesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duhbhavesh"/>
    <language>en</language>
    <item>
      <title>JavaScript Promise methods Explained!</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Thu, 15 Jul 2021 10:00:17 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/javascript-promise-methods-explained-1f0l</link>
      <guid>https://dev.to/duhbhavesh/javascript-promise-methods-explained-1f0l</guid>
      <description>&lt;h3&gt;
  
  
  What is Promise?
&lt;/h3&gt;

&lt;p&gt;The Promise is a simply improved version of callbacks, and JavaScript's Promise is effectively the same as Promise in real life. You make a promise to do something, and either does it and succeed or do not do it and fail. That is how JavaScript's Promise works.&lt;br&gt;
A Promise is essentially saying, "Hey, I'm going to execute some code, and if I'm successful, I'll let you know and give you the result," and "If I'm unsuccessful, I'll let you know and just tell you the error that goes with that 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="c1"&gt;// resolve- happens when you successfully complete the promise&lt;/span&gt;
&lt;span class="c1"&gt;// reject - happens when you failed to complete the promise&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Success&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error&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="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; Success&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;em&gt;Let's look at Promise methods&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise.all()
&lt;/h3&gt;

&lt;p&gt;→ &lt;strong&gt;Promise.all()&lt;/strong&gt; is used to run multiple promises, we need to pass an array of different promises, and then we can do things based on whether they all fail or some of them fail or they all succeed or some of them succeed. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;resolve()&lt;/code&gt;&lt;/strong&gt; → returns a successful promise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;reject()&lt;/code&gt;&lt;/strong&gt; → returns a failed promise.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; ["1", "2", "3"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, all three &lt;code&gt;Promise&lt;/code&gt; have been successfully resolved and they have the message "1", "2", "3" inside of them. The messages array is the return value of all our promises in order from top to bottom.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; Error on 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet only the rejected value is printed from the &lt;code&gt;.catch()&lt;/code&gt; block and nothing gets printed from the &lt;code&gt;.then()&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;This happens because the &lt;code&gt;Promise.all()&lt;/code&gt; only calls &lt;code&gt;.then()&lt;/code&gt; when every single &lt;code&gt;Promise&lt;/code&gt; inside it succeeds or is resolved. If one of them fails it will call &lt;code&gt;.catch&lt;/code&gt; and print the result of the first failed or rejected &lt;code&gt;Promise&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Promise.any()
&lt;/h3&gt;

&lt;p&gt;→ It takes the array of multiple promises and It returns any of the first &lt;code&gt;Promise&lt;/code&gt; that gets succeeds or resolved, you can imagine a bunch of different &lt;code&gt;Promise&lt;/code&gt; taking various amount of time to execute, the first one to get executed will return the value in &lt;code&gt;.then()&lt;/code&gt;&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; 1&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output - 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Promise.race()
&lt;/h3&gt;

&lt;p&gt;→ It takes an array of multiple promises, It is like &lt;code&gt;Promise.any()&lt;/code&gt; but instead of getting the first promise that succeeds, &lt;code&gt;Promise.race()&lt;/code&gt; returns the first &lt;code&gt;Promise&lt;/code&gt; that finishes whether or not it succeeds or fails.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; Error on 1&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is not &lt;code&gt;asynchronous&lt;/code&gt; it is getting executed from top to bottom but if were to imagine it has the timeout and it took a given of time to succeed or fail. The example is given below.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
     &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// 100 ms&lt;/span&gt;
     &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// 400 ms&lt;/span&gt;
     &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// 200 ms&lt;/span&gt;
     &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 20 ms&lt;/span&gt;
&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// Output -&amp;gt; Error on 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet will print &lt;code&gt;Error on 4&lt;/code&gt; because it will be the first one to finish its execution.&lt;/p&gt;




&lt;h3&gt;
  
  
  Promise.allSettled()
&lt;/h3&gt;

&lt;p&gt;→ It takes an array of multiple promises, &lt;code&gt;Promise.allSettled()&lt;/code&gt; waits for all the Promises to finish whether they get rejected or they get fulfilled it doesn't matter it waits for every single &lt;code&gt;Promise&lt;/code&gt; to finish.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allSettled&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;messages&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="cm"&gt;/* Output -&amp;gt; (4) [{…}, {…}, {…}, {…}]
0: {status: "fulfilled", value: "1"}
1: {status: "fulfilled", value: "2"}
2: {status: "rejected", reason: "Error on 3"}
3: {status: "rejected", reason: "Error on 4"}
length: 4
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see &lt;code&gt;Promise.allSettled()&lt;/code&gt; prints the 4 objects and &lt;br&gt;
the object contains the &lt;strong&gt;&lt;code&gt;status&lt;/code&gt;&lt;/strong&gt; which is &lt;em&gt;&lt;code&gt;rejected&lt;/code&gt;&lt;/em&gt; or &lt;em&gt;&lt;code&gt;fullfilled.&lt;/code&gt;&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;reason&lt;/code&gt;&lt;/strong&gt; if the &lt;strong&gt;&lt;code&gt;status&lt;/code&gt;&lt;/strong&gt; is &lt;em&gt;&lt;code&gt;rejected&lt;/code&gt;&lt;/em&gt; &lt;br&gt;
&lt;strong&gt;&lt;code&gt;value&lt;/code&gt;&lt;/strong&gt; if the &lt;strong&gt;&lt;code&gt;status&lt;/code&gt;&lt;/strong&gt; is &lt;em&gt;&lt;code&gt;fulfilled&lt;/code&gt;&lt;/em&gt;.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allSettled&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error on 4&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;messages&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="cm"&gt;/* Output -&amp;gt; (4) [{…}, {…}, {…}, {…}]
0: {status: "rejected", reason: "Error on 1"}
1: {status: "rejected", reason: "Error on 2"}
2: {status: "rejected", reason: "Error on 3"}
3: {status: "rejected", reason: "Error on 4"}
length: 4
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet as you can see it still calls the &lt;code&gt;.then()&lt;/code&gt; even after all the &lt;code&gt;Promise&lt;/code&gt; got rejected, because &lt;code&gt;Promise.allSettled()&lt;/code&gt; will always call &lt;code&gt;.then&lt;/code&gt; even if the &lt;code&gt;Promise&lt;/code&gt; gets fulfilled or rejected.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you for making it till the end!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Closure Explained!</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Wed, 14 Jul 2021 16:23:04 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/closure-explained-2di3</link>
      <guid>https://dev.to/duhbhavesh/closure-explained-2di3</guid>
      <description>&lt;h2&gt;
  
  
  Let's Define Closure
&lt;/h2&gt;

&lt;p&gt;A closure is a function that makes use of variable defined in outer functions that have previously returned. What does this mean, Let's quickly look at an 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="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, the function inner uses the variable "a" declared in a function named "outer," and when the function inner is called, the function outer returns the function called "inner," and this is called as a closure!&lt;/p&gt;

&lt;h2&gt;
  
  
  A few things to note:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We have to 'return' the inner function for the above example to work.&lt;/li&gt;
&lt;li&gt;We can call the inner function right away by using an extra ().&lt;/li&gt;
&lt;li&gt;We don't have to name the inner function (we just called it "inner" for learning purposes)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Closures Work
&lt;/h2&gt;

&lt;p&gt;Only variables used in the inner function are stored!&lt;br&gt;
Closures don't remember everything from an outer function - just the variables they require!&lt;/p&gt;
&lt;h2&gt;
  
  
  Why do I need to know this?
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Private Variables
&lt;/h3&gt;

&lt;p&gt;Variables that cannot be updated externally are supported in other languages. These are referred to as private variables, although they are not included in JavaScript. No need to be concerned - closures can assist!&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;counter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;counter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;counter2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nf"&gt;counter2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="nf"&gt;counter1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 3 this is unaffected by counter2.&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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Uncaught ReferenceError: count is not defined - because it is private!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Closure is when an inner function makes use of variables declared in an outer function that has previously returned. &lt;/li&gt;
&lt;li&gt;JavaScript will only remember values that are being used inside of the inner function, not all variables defined in the outer function.&lt;/li&gt;
&lt;li&gt;Closures allow us to define private variables and write cleaner code that separates our logic from our application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Thank you for making it till the end!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Eliminate Render Blocking JavaScript using async and defer?</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Mon, 31 May 2021 07:43:58 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/how-to-eliminate-render-blocking-javascript-using-async-and-defer-3be4</link>
      <guid>https://dev.to/duhbhavesh/how-to-eliminate-render-blocking-javascript-using-async-and-defer-3be4</guid>
      <description>&lt;h4&gt;
  
  
  What is Render Blocking Resources?
&lt;/h4&gt;

&lt;p&gt;Render blocking resources are static files, such as fonts, HTML, CSS, and &lt;strong&gt;JavaScript&lt;/strong&gt; files, that are vital to the process of rendering a web page. When the browser encounters a render blocking resource, it stops downloading the rest of the resources until these critical files are processed.&lt;/p&gt;




&lt;h4&gt;
  
  
  What is &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt;?
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes are two different ways of loading JavaScript. &lt;/p&gt;

&lt;p&gt;To understand &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; first we need to understand how the &lt;strong&gt;HTML&lt;/strong&gt; is parsed. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt; is parsed from top to bottom and if there is an &lt;br&gt;
&lt;code&gt;&amp;lt;img src="img.jpeg" /&amp;gt;&lt;/code&gt; tag in between it starts downloading the image in the background and the parsing will continue till it reaches the end. &lt;/p&gt;

&lt;p&gt;When it comes to &lt;strong&gt;JavaScript&lt;/strong&gt; it acts a little differently, parsing starts from top to bottom but if it encounters &lt;code&gt;&amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; tag in between the script file gets downloaded and it &lt;strong&gt;stops HTML parsing until it executes the script&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes allow us to modify how this flow works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Normal Parsing -
&lt;/h3&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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-1_izjq2v.jpg" 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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-1_izjq2v.jpg" alt="Normal Parsing Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Normal parsing HTML file is parsed from top to bottom and when it reaches the &lt;code&gt;script&lt;/code&gt; tag it &lt;strong&gt;stops HTML parsing and downloads the script file and executes the script file&lt;/strong&gt;, after downloading and executing it then &lt;strong&gt;continues parsing the HTML file&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    ....
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/em&gt;: When you're not using the &lt;code&gt;async&lt;/code&gt; or &lt;code&gt;defer&lt;/code&gt; attribute it is advised that you should use the &lt;code&gt;&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; tag before the &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; closing body tag.&lt;/p&gt;




&lt;h3&gt;
  
  
  Async Parsing -
&lt;/h3&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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-2_nf8ib3.jpg" 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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-2_nf8ib3.jpg" alt="Async Parsing Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;async&lt;/code&gt; parsing HTML file is parsed from top to bottom and when it reaches the &lt;code&gt;script&lt;/code&gt; tag &lt;strong&gt;instead of stopping, script file is downloaded in the background&lt;/strong&gt; and it &lt;strong&gt;continues to parse the HTML file&lt;/strong&gt; &lt;br&gt;
While the script file is being downloaded in the background, as soon as the &lt;strong&gt;script file is downloaded, HTML parsing is stopped and script file is executed and then it continues parsing the HTML file&lt;/strong&gt; till it reaches the end. &lt;/p&gt;

&lt;p&gt;script with &lt;code&gt;async&lt;/code&gt; attribute can run anytime, there is no guarantee for when it runs, and it can run in any order because they are just downloaded in the background asynchronously, and as soon as they are done downloading they run immediately.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    ....
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h3&gt;
  
  
  Defer Parsing -
&lt;/h3&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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-3_jdva4j.jpg" 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%2Fres.cloudinary.com%2Fduhbhavesh%2Fimage%2Fupload%2Fv1622436692%2Fblogs%2Fasync-defer%2FAsync_Defer-3_jdva4j.jpg" alt="Defer Parsing Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Parsing with &lt;code&gt;defer&lt;/code&gt; attribute is very similar to &lt;code&gt;async&lt;/code&gt; parsing, HTML file is parsed from top to bottom and when it reaches the &lt;code&gt;script&lt;/code&gt; tag &lt;code&gt;script&lt;/code&gt; file gets &lt;strong&gt;downloaded in the background&lt;/strong&gt; and &lt;strong&gt;HTML parsing continues&lt;/strong&gt; but &lt;strong&gt;instead of executing&lt;/strong&gt; the script file after getting downloaded &lt;strong&gt;it waits until the entire page of HTML is parsed&lt;/strong&gt; before it actually executes the script file.&lt;br&gt;
Execution is deferred at the end with &lt;code&gt;defer&lt;/code&gt; attribute.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    ....
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h3&gt;
  
  
  So who's the winner? -
&lt;/h3&gt;

&lt;p&gt;It depends, &lt;br&gt;
use &lt;code&gt;defer&lt;/code&gt; when the order of execution of scripts is important.&lt;br&gt;
use &lt;code&gt;async&lt;/code&gt; when the order of execution of scripts is not important.&lt;/p&gt;




&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; are the great script tag attributes that allow you to speed up the page loading. &lt;br&gt;
Both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; have one common thing: downloading of such scripts doesn’t block page rendering. So the user can read page content and get acquainted with the page immediately.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;async&lt;/th&gt;
&lt;th&gt;defer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;async&lt;/code&gt; attribute blocks the parsing of the page.&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;defer&lt;/code&gt; attribute never blocks the parsing of the page.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution of scripts can be non-sequential.&lt;/td&gt;
&lt;td&gt;Execution of scripts is sequential.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution of scripts leads to pausing of HTML document parsing.&lt;/td&gt;
&lt;td&gt;Execution of scripts is done when HTML document is completely parsed.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Thank you&lt;/em&gt; for making it till the end!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>performance</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is SASS?</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Sat, 10 Oct 2020 11:30:25 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/what-is-sass-1kk1</link>
      <guid>https://dev.to/duhbhavesh/what-is-sass-1kk1</guid>
      <description>&lt;p&gt;Once you’re comfortable with CSS, the next natural step is to use a preprocessor. The biggest advantage is not having to repeat yourself. “Don’t Repeat Yourself” or "DRY" is a principle in software development aimed at reducing repetition of code, and using SASS we can have the advantage of keeping our CSS Dry.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is CSS preprocessor?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;"A CSS preprocessor is a program that lets you generate CSS from the preprocessor's own unique syntax."&lt;/em&gt; - &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/CSS_preprocessor" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why use preprocessors?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cleaner code using variables and nesting.&lt;/li&gt;
&lt;li&gt;Ease of organizing and maintaining the code as we can separate files into modules.&lt;/li&gt;
&lt;li&gt;Ability to implement logic and calculations in stylesheets.&lt;/li&gt;
&lt;li&gt;Overall improvement in workflow which can save hours of development time!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is SASS?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;SASS (or Syntactically Awesome Style Sheets) is a tool known as a CSS preprocessor. SASS is a scripting language that provides us the capabilities which regular CSS doesn't have. Using SASS we can write more maintainable, readable and reusable code.&lt;/p&gt;

&lt;p&gt;It gives us various capabilities such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Partials &amp;amp; Imports&lt;/li&gt;
&lt;li&gt;Nesting&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Mixins&lt;/li&gt;
&lt;li&gt;Control directives&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words SASS helps us write CSS and organize large stylesheets in more maintainable way.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>sass</category>
      <category>css</category>
      <category>webdev</category>
      <category>scss</category>
    </item>
    <item>
      <title>nvm command not found :(</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Wed, 05 Aug 2020 14:59:06 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/nvm-command-not-found-1ho</link>
      <guid>https://dev.to/duhbhavesh/nvm-command-not-found-1ho</guid>
      <description>&lt;p&gt;Node Version Manager (NVM) is a tool used to manage multiple active Node.js versions.&lt;/p&gt;

&lt;p&gt;If you're trying to install NVM(Node Version Manager) in WSL you probably may run into this error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-bash&lt;/span&gt;: nvm: &lt;span class="nb"&gt;command &lt;/span&gt;not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is WSL ?
&lt;/h3&gt;

&lt;p&gt;The Windows Subsystem for Linux lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to fix this error ?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To install nvm use the following cURL or Wget command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bash curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget &lt;span class="nt"&gt;-qO-&lt;/span&gt; https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;nvm command not found&lt;/strong&gt; :(&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Running either of the above commands downloads a script and runs it, but if you type nvm and if you get &lt;code&gt;bash: nvm: command not found&lt;/code&gt; it happens because the source is missing from the following profile files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 To fix this simply follow these steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Steps&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;1) Type the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Add the source lines from the snippet below to the correct profile file at the bottom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;NVM_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-z&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;XDG_CONFIG_HOME&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; %s &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;HOME&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/.nvm"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;printf&lt;/span&gt; %s &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;XDG_CONFIG_HOME&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/nvm"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NVM_DIR&lt;/span&gt;&lt;span class="s2"&gt;/nvm.sh"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NVM_DIR&lt;/span&gt;&lt;span class="s2"&gt;/nvm.sh"&lt;/span&gt; &lt;span class="c"&gt;# This loads nvm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Close and restart the terminal it should fix the error!&lt;/p&gt;

&lt;p&gt;The official documentation on &lt;a href="https://github.com/nvm-sh/nvm#installing-and-updating" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; is really helpful.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>node</category>
      <category>nvm</category>
      <category>wsl</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Time Zone issue with Heroku</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Tue, 30 Jun 2020 10:44:36 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/time-zone-issue-with-heroku-21d5</link>
      <guid>https://dev.to/duhbhavesh/time-zone-issue-with-heroku-21d5</guid>
      <description>&lt;p&gt;I've been using &lt;a href="https://heroku.com" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; for a while now, Recently I was working on a project which was a basic chat application, It was built with &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Nodejs&lt;/a&gt;, &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;socket.io&lt;/a&gt;, &lt;a href="https://momentjs.com/" rel="noopener noreferrer"&gt;momentjs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As it was a chat application I used momentjs for the timestamps on the messages, Everything was working fine locally, but when I deployed my app on Heroku I found that messages were not showing the correct time.&lt;/p&gt;

&lt;p&gt;As the Heroku's default region is set to &lt;strong&gt;US&lt;/strong&gt; &amp;amp; I was from &lt;strong&gt;India&lt;/strong&gt; that was causing the issue. So basically momentjs was showing &lt;strong&gt;US Timezone&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So the problem was with Heroku's Timezone &amp;amp; Luckily Heroku allows us to change the timezone of our app just by simply adding a new config var.&lt;br&gt;
After fixing the problem it was working fine :)&lt;/p&gt;

&lt;p&gt;Here's the Solution:&lt;/p&gt;
&lt;h4&gt;
  
  
  Heroku Dashboard - Website
&lt;/h4&gt;

&lt;p&gt;1- Login into the Heroku from a browser&lt;br&gt;
2- Select your app&lt;br&gt;
3- Go to the setting tab&lt;br&gt;
4- Press "Reveal Config vars" button&lt;br&gt;
5- Set the keys to "TZ" and value of your timezone (Eg: Asia/Kolkata)&lt;br&gt;
6- Press the add button&lt;/p&gt;
&lt;h4&gt;
  
  
  Heroku CLI
&lt;/h4&gt;

&lt;p&gt;1- Open your Command shell&lt;br&gt;
2- Login into Heroku&lt;br&gt;
3- cd into your App directory&lt;br&gt;
4- Setup TZ&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;heroku config:add &lt;span class="nv"&gt;TZ&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Asia/Kolkata"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is correct TZ value of your country ?
&lt;/h3&gt;

&lt;p&gt;You can find your country's TZ value from this &lt;a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="noopener noreferrer"&gt;wiki&lt;/a&gt; page&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;India uses a single time zone for the whole country.&lt;/strong&gt;&lt;br&gt;
I looked on the Wiki page List of tz database time zones, but I was unable to find an entry for India.&lt;/p&gt;

&lt;p&gt;If you can't find your country's TZ follow this &lt;a href="https://github.com/eggert/tz/blob/master/zone1970.tab" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thanks for reading!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>momentjs</category>
      <category>heroku</category>
      <category>node</category>
    </item>
    <item>
      <title>Why are my commits aren't showing up on GitHub contributions graph?</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Sat, 27 Jun 2020 06:43:11 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/why-my-commits-aren-t-showing-up-on-github-contributions-graph-3a2h</link>
      <guid>https://dev.to/duhbhavesh/why-my-commits-aren-t-showing-up-on-github-contributions-graph-3a2h</guid>
      <description>&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%2Fdiml2jp6c7nzt38jnamn.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%2Fdiml2jp6c7nzt38jnamn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So recently, I started using GitHub after building projects &amp;amp; after pushing my code on GitHub I was having an issue with my contributions graph - All of my commits were not showing up.&lt;/p&gt;

&lt;p&gt;After lots of googling, support emails &amp;amp; help from people who are knowledgeable about git on discord community I figured out the issue.&lt;/p&gt;

&lt;p&gt;There's the official GitHub article on &lt;a href="https://help.github.com/en/github/setting-up-and-managing-your-github-profile/why-are-my-contributions-not-showing-up-on-my-profile" rel="noopener noreferrer"&gt;Why are my contributions not showing up on my profile?&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Commits&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Commits will appear on your contributions graph if they meet all of the following conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The email address used for the commits is associated with your GitHub account.&lt;/li&gt;
&lt;li&gt;The commits were made in a standalone repository, not a fork.&lt;/li&gt;
&lt;li&gt;The commits were made:&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;So after going through this GitHub doc, I found that the actual issue was with the Condition 1 -&lt;br&gt;
&lt;code&gt;The email address used for the commits is associated with your GitHub account&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The issue I was experiencing was with the GitHub Desktop, email address wasn't matching with my GitHub account. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My GitHub email looked like &lt;code&gt;test.name.ok@gmail.com&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;My GitHub Desktop email was &lt;code&gt;test-name-ok@gmail.com&lt;/code&gt; 
this was the issue incorrect email!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're experiencing the same issue head over to the options tab in GitHub Desktop and make sure your desktop email is similar to your GitHub account. &lt;/p&gt;

&lt;p&gt;Steps: File &amp;gt; Options &amp;gt; Git&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%2Flh3.googleusercontent.com%2Fbp3XqN4qu_gpP15rGi1lp4MejzV2-rPiPjGaJP3-iyUBVzn_x8ZC2zfj56KrZSt5a6pWwVKsgpHUEg%3Dw2940-h5226" 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%2Flh3.googleusercontent.com%2Fbp3XqN4qu_gpP15rGi1lp4MejzV2-rPiPjGaJP3-iyUBVzn_x8ZC2zfj56KrZSt5a6pWwVKsgpHUEg%3Dw2940-h5226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once I figured out the cause of the issue, I needed to fix my contributions graph so all of my commits will be back on my graph. I ran this &lt;a href="https://stackoverflow.com/questions/750172/how-to-change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-gi" rel="noopener noreferrer"&gt;script&lt;/a&gt; on each of my repositories to update the user info to the correct email address, voila! my commits were showing on the graph!&lt;/p&gt;

&lt;h4&gt;
  
  
  How to know if this is the issue you're facing?
&lt;/h4&gt;

&lt;p&gt;Check your commit history for one of your repositories, If you don't see your avatar on your commits next to different commits, but you have avatar on your GitHub profile, you're probably not using the correct email for your commits.&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%2Flh5.googleusercontent.com%2FgxWLlxyp_13rFjVqM_a2YxBg5EaMDqxGUfHfUyGqcPyWMus0uvsN67krSRb4pAO3-bhtjx98oa4N7A%3Dw1366-h397-rw" 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%2Flh5.googleusercontent.com%2FgxWLlxyp_13rFjVqM_a2YxBg5EaMDqxGUfHfUyGqcPyWMus0uvsN67krSRb4pAO3-bhtjx98oa4N7A%3Dw1366-h397-rw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above shows a portion of the commit history for one of my repositories. After I fixed the email issue, all of those commits showed my avatar.&lt;/p&gt;

&lt;p&gt;After I fixed the email issue, all of those commits showed my avatar, As you can see in below image.&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%2Flh5.googleusercontent.com%2FGWl6fGsETY5GWBCtPe1p6BGr5UttYaziNCdGWVgWWPPHCabZsE67T4q4QNPOZvrjQuNJLtr2JQPrfQ%3Dw2940-h5226-rw" 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%2Flh5.googleusercontent.com%2FGWl6fGsETY5GWBCtPe1p6BGr5UttYaziNCdGWVgWWPPHCabZsE67T4q4QNPOZvrjQuNJLtr2JQPrfQ%3Dw2940-h5226-rw"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If you are not seeing your contributions on your graph, check that you are meeting all &lt;a href="https://help.github.com/en/github/setting-up-and-managing-your-github-profile/why-are-my-contributions-not-showing-up-on-my-profile" rel="noopener noreferrer"&gt;conditions&lt;/a&gt;, and be aware that it may be an issue with your email address used/not used correctly in the GitHub Desktop you use for version control on your local development environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thank you for reading&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to use Fetch API for CRUD operations?</title>
      <dc:creator>Bhavesh Kasturi</dc:creator>
      <pubDate>Mon, 01 Jun 2020 08:00:05 +0000</pubDate>
      <link>https://dev.to/duhbhavesh/how-to-use-fetch-api-for-crud-operations-57a0</link>
      <guid>https://dev.to/duhbhavesh/how-to-use-fetch-api-for-crud-operations-57a0</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;What is CRUD Operation&lt;/strong&gt;?
&lt;/h1&gt;

&lt;p&gt;The acronym &lt;strong&gt;CRUD&lt;/strong&gt; stands for &lt;strong&gt;Create&lt;/strong&gt;, &lt;strong&gt;Read&lt;/strong&gt;, &lt;strong&gt;Update&lt;/strong&gt; and &lt;strong&gt;Delete&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Create&lt;/em&gt;&lt;/strong&gt;: Inserts a new data&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Read&lt;/em&gt;&lt;/strong&gt;: Read the data&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Update&lt;/em&gt;&lt;/strong&gt;: Update the existing data&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Delete&lt;/em&gt;&lt;/strong&gt;: Delete the existing data&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;List of HTTP Request methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;GET&lt;/em&gt;&lt;/strong&gt; - is used to request data from a specified resource.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;POST&lt;/em&gt;&lt;/strong&gt; - is used to send data to a server to create a resource.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;PUT&lt;/em&gt;&lt;/strong&gt; - is used to send data to a server to update a resource.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;DELETE&lt;/em&gt;&lt;/strong&gt; - is used to delete the specified resource.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is REST API Server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you're performing CRUD operation using Fetch API you're going to need a REST API server&lt;br&gt;
For learning purposes we can use &lt;a href="https://jsonplaceholder.typicode.com/" rel="noopener noreferrer"&gt;jsonplaceholder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSONPlaceholder&lt;/strong&gt; is a free online REST API that you can use whenever you need some fake data. It's great for learning, tutorials, testing new libraries, sharing code-examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Fetch API&lt;/strong&gt;
&lt;/h2&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%2Flh6.googleusercontent.com%2FNsRmwChD7uCgqPmXa7sc7_vDRPawh9Hp5m3bstvZWm7-uXSie86zvXv2nW_fJYPmN6fbFTvJ4lKecw%3Dw1366-h625-rw" 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%2Flh6.googleusercontent.com%2FNsRmwChD7uCgqPmXa7sc7_vDRPawh9Hp5m3bstvZWm7-uXSie86zvXv2nW_fJYPmN6fbFTvJ4lKecw%3Dw1366-h625-rw" alt="fetch api"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;GET Posts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;url&lt;/em&gt;&lt;/strong&gt; - &lt;a href="https://jsonplaceholder.typicode.com/posts" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/posts&lt;/a&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%2Flh6.googleusercontent.com%2FbxL2LbkkSTlH4guc8aTGL1JGqhWylpOmMXkvt8jivWc6dojXsG5Rx4mqQorIUyWyoTAB2okKINPHbA%3Dw1366-h625-rw" 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%2Flh6.googleusercontent.com%2FbxL2LbkkSTlH4guc8aTGL1JGqhWylpOmMXkvt8jivWc6dojXsG5Rx4mqQorIUyWyoTAB2okKINPHbA%3Dw1366-h625-rw" alt="get posts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Console
&lt;/h3&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%2Flh4.googleusercontent.com%2F5sL3e63Ee1aR8nIYC021GABcb1J_OpG_BabZLwIRsSPK8RGAt238TOVMwDyUfkjYVNKc8CDZSf8qDQ%3Dw1366-h625" 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%2Flh4.googleusercontent.com%2F5sL3e63Ee1aR8nIYC021GABcb1J_OpG_BabZLwIRsSPK8RGAt238TOVMwDyUfkjYVNKc8CDZSf8qDQ%3Dw1366-h625" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;CREATE a Post&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;url&lt;/em&gt;&lt;/strong&gt; - &lt;a href="https://jsonplaceholder.typicode.com/posts" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/posts&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;methods&lt;/em&gt;&lt;/strong&gt; - POST&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%2Flh5.googleusercontent.com%2F_dRQkVtC43_H3SgH_Kns6vppM-wGTPD5sKdId2tIil-86dgsAVIQawpMvIFkcgDYHEsZWAr-H-Phvg%3Dw1366-h625-rw" 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%2Flh5.googleusercontent.com%2F_dRQkVtC43_H3SgH_Kns6vppM-wGTPD5sKdId2tIil-86dgsAVIQawpMvIFkcgDYHEsZWAr-H-Phvg%3Dw1366-h625-rw" alt="create post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;UPDATE a Post&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;url&lt;/em&gt;&lt;/strong&gt; - &lt;a href="https://jsonplaceholder.typicode.com/posts" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/posts&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;methods&lt;/em&gt;&lt;/strong&gt; - PUT&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%2Flh5.googleusercontent.com%2FNwpb20sVJwonf9AdEPx0DzGYPzU1MNQDvbI5ga6dFUWMxudWUXKQe8IjQE_dmVwQqhjsUl13KvG2mQ%3Dw1366-h625-rw" 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%2Flh5.googleusercontent.com%2FNwpb20sVJwonf9AdEPx0DzGYPzU1MNQDvbI5ga6dFUWMxudWUXKQe8IjQE_dmVwQqhjsUl13KvG2mQ%3Dw1366-h625-rw" alt="update post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;DELETE a Post&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;url&lt;/em&gt;&lt;/strong&gt; - &lt;a href="https://jsonplaceholder.typicode.com/posts/0" rel="noopener noreferrer"&gt;https://jsonplaceholder.typicode.com/posts/0&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;&lt;code&gt;0 is a post id, so we are going to delete a post where the id = 0&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;methods&lt;/em&gt;&lt;/strong&gt; - DELETE&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%2Flh4.googleusercontent.com%2FBzHo9nqPqVSksdfYa--2-e7tH1q26Sn9GaQzoGTW1h13I60dlrXkPQfe8mKXLPdXwWzVWLVBMWXa7w%3Dw1366-h302-rw" 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%2Flh4.googleusercontent.com%2FBzHo9nqPqVSksdfYa--2-e7tH1q26Sn9GaQzoGTW1h13I60dlrXkPQfe8mKXLPdXwWzVWLVBMWXa7w%3Dw1366-h302-rw" alt="delete post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;JS HTTP Request Libraries&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To help make our experience with AJAX and XMLHttpRequest a nice one. Libraries have been developed to help us make HTTP requests without dealing with complexities of AJAX and XMLHttpRequest.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;- Axios&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Promise based HTTP client for the browser and node.js&lt;/p&gt;

&lt;p&gt;This is a Promise-based HTTP library for performing HTTP requests on both Nodejs and Browser. It supports all modern browser, even an included support for IE8 +.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/axios/axios" rel="noopener noreferrer"&gt;https://github.com/axios/axios&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;- SuperAgent&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is a Promise-based light-weight progressive AJAX API perfectly suited for sending HTTP requests and receiving server responses. Like axios, it works in both Node and in all modern browsers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/visionmedia/superagent" rel="noopener noreferrer"&gt;https://github.com/visionmedia/superagent&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;- Supertest&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Super-agent driven library for testing node.js HTTP servers using a fluent API&lt;/p&gt;

&lt;p&gt;This is used for testing Node.js HTTP servers. This library is powered by SuperAgent, it combines its own API and the lower-level API provided by SuperAgent to provide a neat interface for testing HTTP.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/visionmedia/supertest" rel="noopener noreferrer"&gt;https://github.com/visionmedia/supertest&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;- Qwest&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Qwest is a simple ajax library based on promises, and that supports &lt;code&gt;XmlHttpRequest2&lt;/code&gt; unique data like ArrayBuffer, Blob, and FormData.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/pyrsmk/qwest" rel="noopener noreferrer"&gt;https://github.com/pyrsmk/qwest&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;Thanks for reading!&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>react</category>
    </item>
  </channel>
</rss>
