<?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: Vivek Kumar Gupta</title>
    <description>The latest articles on DEV Community by Vivek Kumar Gupta (@vivek_gupta).</description>
    <link>https://dev.to/vivek_gupta</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%2F1227957%2Fde82b0c3-3bd0-4f36-be73-c9fa2d120329.jpeg</url>
      <title>DEV Community: Vivek Kumar Gupta</title>
      <link>https://dev.to/vivek_gupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vivek_gupta"/>
    <language>en</language>
    <item>
      <title>Let's Understand Promises in JavaScript</title>
      <dc:creator>Vivek Kumar Gupta</dc:creator>
      <pubDate>Sun, 10 Dec 2023 10:03:17 +0000</pubDate>
      <link>https://dev.to/vivek_gupta/lets-understand-promises-in-javascript-h9n</link>
      <guid>https://dev.to/vivek_gupta/lets-understand-promises-in-javascript-h9n</guid>
      <description>&lt;h2&gt;
  
  
  What are Promises?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Promise in Javascript is same as we commit a promise in real life. As we all know that promise is made for future, so definately, the outcome will also come in future, which makes the present state of promise is &lt;em&gt;pending&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Their are 2 Possible outcomes of a Promise, either the promise will &lt;em&gt;maintained&lt;/em&gt; or else it will be &lt;em&gt;broken&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similarly, In JavaScript, their are 2 possible outcomes of a Promise: &lt;em&gt;Resolve&lt;/em&gt; or &lt;em&gt;Reject&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Let's understand this statement with an example.
&lt;/h2&gt;

&lt;p&gt;Suppose, You (Ram) and your friend (Sham) just passed Higher Secondary School and because you both are best friends, so you both decided take admission in the same college to do your bachelor's.&lt;/p&gt;

&lt;p&gt;Now, there can be 2 possibility that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ram and Sham took admission in same college, which means promise is fulfilled or in other words &lt;em&gt;Promise is Resolved&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Because of some reason Sham decided join his father's company directly after high school, which means promise which they made is broken or in other words &lt;em&gt;Promise is Rejected&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Different States of Promise in JavaScript
&lt;/h2&gt;

&lt;p&gt;When a Promised is created in JavaScript, it is first initialised with a state of &lt;em&gt;pending.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending (Initial State, before the Promise succeeds or fails)&lt;/li&gt;
&lt;li&gt;Resolved (Completed Promise)&lt;/li&gt;
&lt;li&gt;Rejected (Failed Promise)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcb965rxbxn1g3r8hlfr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcb965rxbxn1g3r8hlfr3.png" alt="Descriptive picture of promise states" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What is the need of Promises?
&lt;/h2&gt;

&lt;p&gt;So, Vivek tell me one thing why do we need promises in javaScript instead why can't we use callbacks to make async calls?&lt;/p&gt;

&lt;p&gt;Okay, So the answer of your question is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have &lt;em&gt;callback&lt;/em&gt; functions in JavaScript. But, a callback is not a special thing in JavaScript. It is a regular function that produces results after an asynchronous call completes (with success/error).&lt;/li&gt;
&lt;li&gt;While callbacks are helpful, there is a huge downside to them as well. At times, we may have one callback inside another callback that's in yet another callback and so on, now you're trapped in a &lt;em&gt;callback hell&lt;/em&gt;. I'm serious! Let's understand this "callback hell" with an example.
&lt;/li&gt;
&lt;/ul&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;displayMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inside displayMessage&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1st work done!&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2nd work done!&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3rd work done!&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;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

     &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;all works are done!!&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onDone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statement&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;statement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;displayMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onDone&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, Now lets understand the code line by line:&lt;/p&gt;

&lt;p&gt;In this example a function displayMessage is called which recieves a callback function, this displayMessage functions does a simple work that it logs that status of work completions. As we can see that there is a nesting of setTimeout inside every setTimeout(), this nesting can go on, this nesting of function inside another function is called callback hell.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing this type of code can create many production grade implication like: 

&lt;ul&gt;
&lt;li&gt;it makes the code performance slow, &lt;/li&gt;
&lt;li&gt;it impacts the readability of code, etc...&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is the reason why Promises are introduced&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Let's Understand how to write a Promise.
&lt;/h2&gt;

&lt;p&gt;We have to create an instace of Promise class; Let's see how we can do that;&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;let&lt;/span&gt; &lt;span class="nx"&gt;p&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;// your code&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The constructor function takes an executor function which takes two arguments which are &lt;em&gt;resolve&lt;/em&gt; and &lt;em&gt;reject&lt;/em&gt; repectively.&lt;/p&gt;

&lt;p&gt;Whenever the promise is resolved successfully their after we call the resolve parameter with the result.&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;myPromise&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;p&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="nf"&gt;setTimeout&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="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;promise is resolved :)&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;2000&lt;/span&gt;&lt;span class="p"&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;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nf"&gt;myPromise&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever the promise is not resolved their after we call the reject parameter with a error.&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;myPromise&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;p&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="nf"&gt;setTimeout&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="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;promise failed to resolved :(&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;2000&lt;/span&gt;&lt;span class="p"&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;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nf"&gt;myPromise&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to get the output of the promise their are 2 methods: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;.then syntax&lt;/li&gt;
&lt;li&gt;async/await syntax &lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;let me tell you that async/await syntax is most prefered way to get the output of a Promise.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's us see how we can get the output using .then method&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;myPromise&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&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="nf"&gt;setTimeout&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="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;promise resolved :)&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;2000&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;myPromise&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;data&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="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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//output: &lt;/span&gt;
  &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="nx"&gt;resolved&lt;/span&gt; &lt;span class="p"&gt;:)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, while using this method .then takes a callback function which recives the data that is returned by the function. Inside that function we can perform any operation which is required by the program.&lt;/p&gt;

&lt;p&gt;Let's us see how we can get the output using async/await method&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;myPromise&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&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="nf"&gt;setTimeout&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="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;promise resolved :)&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;2000&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getResult&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;myPromise&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;getResult&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 javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//output: &lt;/span&gt;
  &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="nx"&gt;resolved&lt;/span&gt; &lt;span class="p"&gt;:)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, while using async/await syntax we initialise an async function inside which we call the myPromise function with await keyword before it.&lt;/p&gt;

&lt;p&gt;This await keywords stops the excetion further down the line until the promise is not resolved.&lt;/p&gt;

&lt;p&gt;As soon as the promise is resolved we can perform any operation which we want, in this case we have logged the data.&lt;/p&gt;




&lt;h2&gt;
  
  
  How errors are handled in promises
&lt;/h2&gt;

&lt;p&gt;Both .then and async/await have different syntax for error handling.&lt;/p&gt;

&lt;p&gt;First let's dig into .then() syntax.&lt;/p&gt;

&lt;p&gt;Whenever we use .then() to consume the output of the promise we use .catch to handle the error.&lt;/p&gt;

&lt;p&gt;Let's see how?&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="c1"&gt;// refering to above example&lt;/span&gt;
&lt;span class="c1"&gt;// if promise is rejected then .catch catches the error&lt;/span&gt;

&lt;span class="nf"&gt;myPromise&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;data&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="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="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="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;// whatever errror we get&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;error&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;using this way the error is handle.&lt;/p&gt;

&lt;p&gt;But when we use Async/Await we write our code inside something known as try/catch block&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="c1"&gt;// refering to above example&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="k"&gt;try&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;myPromise&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="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="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// whatever errror we get&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;error&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;consumer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Advantage over Promises over Callbacks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Better Readability&lt;/strong&gt;: Promises are a more structured and readable way of handling asynchronous operations. Promises allow you to chain multiple asynchronous operations together, Which makes the code more concise and clean as well as less bug-prone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CallBack Hell&lt;/strong&gt;: Promises help you to avoid callback hell, which occurs when you have nested callbacks that can become more difficult to find out bugs and manage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Promises have built-in error-handling mechanisms to allow you to handle errors more easily.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value return Ability&lt;/strong&gt;: Promises allow you to return a value from an asynchronous operation, That can be helpful when you try to pass data from one asynchronous to another.&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Hope this article clears the topic&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;If you like this article then please like, share it on socials, comment your thoughts&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Let's Talk about Event Loop in JavaScript</title>
      <dc:creator>Vivek Kumar Gupta</dc:creator>
      <pubDate>Thu, 07 Dec 2023 07:34:06 +0000</pubDate>
      <link>https://dev.to/vivek_gupta/lets-talk-about-event-loop-in-javascript-c4k</link>
      <guid>https://dev.to/vivek_gupta/lets-talk-about-event-loop-in-javascript-c4k</guid>
      <description>&lt;h2&gt;
  
  
  Before knowing what is Event Loop, we need to understand the nature of Javascript.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As we all know that JavaScript is a &lt;strong&gt;Single-threaded&lt;/strong&gt; language which means that it can excecute only a single line code at a time unlike other programming languages like C++/Java.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript engine excecutes the code from top to bottom. It creates an execution context, and inserts, removes the function in and out of the &lt;strong&gt;call stack&lt;/strong&gt; while the code is getting executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While the code is getting executed if it strikes a function that performs a very long task then rest of the code cannot be executed until the current function is not completed its execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lets take an example to understand this point -&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdwgf1v860y58eufstus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdwgf1v860y58eufstus.png" alt="Example of a code which demonstrates how js work on a single thread" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explaination:

&lt;ul&gt;
&lt;li&gt;Output of the code:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Start of the program
   program completed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In this code, we can see that the first &lt;strong&gt;console.log()&lt;/strong&gt; 
printed as soon the code started to geeting execute but inside 
the function &lt;strong&gt;runner()&lt;/strong&gt;, the while loop blocks the execution 
the last &lt;strong&gt;console.log()&lt;/strong&gt;, this a perfect example which 
explains that javascript is single-threaded language.&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;To solve this problem event loop comes into picture with the help of which Javascript performs &lt;strong&gt;asynchronous&lt;/strong&gt; tasks/operations and creates an illusion of &lt;strong&gt;multi-threading&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Components of a Web Browser -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Runtime

&lt;ul&gt;
&lt;li&gt;Heap Memory&lt;/li&gt;
&lt;li&gt;Call Stack&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Web APIs&lt;/li&gt;

&lt;li&gt;Event Loop&lt;/li&gt;

&lt;li&gt;Queues:

&lt;ul&gt;
&lt;li&gt;MicroTask Queue&lt;/li&gt;
&lt;li&gt;MacroTask Queue (aka. Callback Queue) &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Entry of Callback functions
&lt;/h2&gt;

&lt;p&gt;To prevent a blocking function from blocking other activities, we typically put it inside a &lt;strong&gt;callback&lt;/strong&gt; function to execute it after all other activities are are performed by the code.&lt;/p&gt;

&lt;p&gt;Now, you are wondering that what the heck is a Callback function?&lt;/p&gt;

&lt;p&gt;As mentioned that earlier that Web browser have other components as well other than JS Engine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To Understand Callback function lets take an example.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidhx324rxkubngv7w1mi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidhx324rxkubngv7w1mi.png" alt="example of a callback function" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you call the &lt;em&gt;setTimeout()&lt;/em&gt; function, make a &lt;em&gt;fetch request&lt;/em&gt;, or &lt;em&gt;click a button&lt;/em&gt;, the web browser can do these activities concurrently and asynchronously.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;setTimeout()&lt;/em&gt;, &lt;em&gt;fetch requests&lt;/em&gt; and &lt;em&gt;DOM events&lt;/em&gt; are parts of the Web APIs of the web browser.&lt;/li&gt;
&lt;li&gt;In our example, when calling the &lt;em&gt;setTimeout()&lt;/em&gt; function, the JavaScript engine places it on the &lt;em&gt;call stack&lt;/em&gt;, and the Web API creates a &lt;em&gt;timer&lt;/em&gt; that expires in 1 second, till then rest of the code gets executed.&lt;/li&gt;
&lt;li&gt;Then JavaScript engine places the task() function into a queue called a &lt;em&gt;callback queue&lt;/em&gt; or a &lt;em&gt;task queue&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;This is where &lt;em&gt;Event Loop&lt;/em&gt; comes into picture&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;Event loop&lt;/em&gt; facilitates this process,

&lt;ul&gt;
&lt;li&gt;It constantly checks whether or not the call stack is empty. If it is empty, new functions are added from the &lt;em&gt;event queue&lt;/em&gt;. If it is not, then the current function call is processed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Hope the topic was cleared and now you have a better understanding of Event loop in JavaScript&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Thankyou&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
