<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Javascript on Nitesh Raya&#39;s Blogs</title>
        <link>/tags/javascript/</link>
        <description>Recent content in Javascript on Nitesh Raya&#39;s Blogs</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <copyright>nitesh</copyright>
        <lastBuildDate>Tue, 31 Dec 2024 00:00:00 +0000</lastBuildDate><atom:link href="/tags/javascript/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>How to use Promise in Javascript</title>
        <link>/post/how-to-use-promise-in-javascript/</link>
        <pubDate>Tue, 31 Dec 2024 00:00:00 +0000</pubDate>
        
        <guid>/post/how-to-use-promise-in-javascript/</guid>
        <description>&lt;img src="/images/promise_thumbnail.jpg" alt="Featured image of post How to use Promise in Javascript" /&gt;&lt;p&gt;Before learning about promise it&amp;rsquo;s important to know what &lt;a class=&#34;link&#34; href=&#34;https://en.wikipedia.org/wiki/Asynchrony_%28computer_programming%29&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;Asynchronous programming&lt;/a&gt; .&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In asynchronous programming, the program continues to execute tasks without waiting for other program to complete any task. It is independent and it uses &lt;strong&gt;callbacks&lt;/strong&gt;, &lt;strong&gt;==promises==&lt;/strong&gt; to wait for the tasks. This technique allows the program to run concurrently and use resources efficiently.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div style=&#34;text-align: center; padding: 20px; border: 2px solid #ffffff; border-radius: 8px; font-family: Arial, sans-serif; background-color: #002b26; color: #ffffff;&#34;&gt;
    &lt;h1 style=&#34;font-size: 2rem; margin-bottom: 20px;&#34;&gt;&#34;I Promise a Result!&#34;&lt;/h1&gt;
    &lt;p style=&#34;font-size: 1.2rem; margin: 10px 0;&#34;&gt;&#34;Producing code&#34; is code that can take some time&lt;/p&gt;
    &lt;p style=&#34;font-size: 1.2rem; margin: 10px 0;&#34;&gt;&#34;Consuming code&#34; is code that must wait for the result&lt;/p&gt;
    &lt;p style=&#34;font-size: 1.2rem; margin: 10px 0;&#34;&gt;A Promise is an Object that links Producing code and Consuming code&lt;/p&gt;
&lt;/div&gt;
&amp;nbsp;
&lt;div style=&#34;display: flex; align-items: center; background-color: #0f172a; border-radius: 8px; padding: 15px; border-left: 4px solid #3b82f6; color: #cbd5e1;&#34;&gt;
    &lt;div style=&#34;color: #3b82f6; font-size: 24px; margin-right: 12px; line-height: 1;&#34;&gt;&amp;#9432;&lt;/div&gt;
    &lt;div style=&#34;flex: 1; font-size: 16px;&#34;&gt;
        &lt;strong&gt;Note:&lt;/strong&gt; Arrow function expressions can have an 
        &lt;a href=&#34;#&#34; style=&#34;color: #3b82f6; text-decoration: underline;&#34;&gt;implicit return&lt;/a&gt;; so, 
        &lt;code style=&#34;background-color: #1e293b; padding: 2px 6px; border-radius: 4px; color: #cbd5e1; font-family: monospace;&#34;&gt;() =&amp;gt; x&lt;/code&gt; 
        is short for 
        &lt;code style=&#34;background-color: #1e293b; padding: 2px 6px; border-radius: 4px; color: #cbd5e1; font-family: monospace;&#34;&gt;() =&amp;gt; { return x; }
        &lt;/code&gt;.
    &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;a-simple-example-code-in-javascript&#34;&gt;A simple example code in Javascript
&lt;/h2&gt;&lt;p&gt;We are using fetch API to get the data from the endpoint and then chaining the result.&lt;/p&gt;
&lt;h3 id=&#34;how-it-works&#34;&gt;How it works
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;First the fetch return a &lt;strong&gt;promise&lt;/strong&gt; that resolves to Response object if success or Reject if failed.&lt;/li&gt;
&lt;li&gt;Our first &lt;code&gt;.then&lt;/code&gt; block processes the resolved Response object.Here either error or the &lt;strong&gt;promise&lt;/strong&gt; is thrown.&lt;/li&gt;
&lt;li&gt;Our second &lt;code&gt;.then&lt;/code&gt; block processes the parsed JSON data.&lt;/li&gt;
&lt;li&gt;Our last chain catches error if there&amp;rsquo;s any.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;jsonData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;items&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;fetch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;https://dummyjson.com/products&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//returns a Response or Reject Promise object.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;then&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;response&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;		&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;!&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;response&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;){&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;			&lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Response network error&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//returns error
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;		&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;		&lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;response&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;json&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// returns json promise
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;then&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;		&lt;span class=&#34;nx&#34;&gt;jsonData&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;catch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;error&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;		&lt;span class=&#34;nx&#34;&gt;console&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nx&#34;&gt;console&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;log&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;jsonData: &amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;jsonData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// output: undefined [i.e this gets executed before the data is assigned to the jsonData]
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Our above example is a good example of using promise but there is a small bug where the data is not stored correctly in the &lt;code&gt;jsonData&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why the heck is the data not being stored in our variable?&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This is because the whole point of promise is to let other synchronous code run and not block the rest of the program to execute. It is a feature and not a bug.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Let&amp;rsquo;s understand this&lt;/strong&gt;
when the &lt;code&gt;fetch()&lt;/code&gt; function is called , it is supposed to return a new Promise object, which takes times as it has to connect to the network and get the data right? So, our code doesn&amp;rsquo;t get blocked, it instead leaves all the things inside the &lt;code&gt;fetch()&lt;/code&gt; function like other &lt;code&gt;.then() and .catch()&lt;/code&gt; to run later after the &lt;code&gt;fetch()&lt;/code&gt; resolves the result and directly moves to execute other part of the code, which in our case is &lt;code&gt;console.log(&amp;quot;jsonData: &amp;quot;+jsonData);&lt;/code&gt; since nothing has been returned by the promise yet the output results to &lt;code&gt;undefined&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to fix this ?&lt;/strong&gt;
The easiest fix is to log the data inside the second &lt;code&gt;.then&lt;/code&gt; block, which then makes sure that the data is initialized and logged only after.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;then&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;	
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;	&lt;span class=&#34;nx&#34;&gt;console&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;log&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt; &lt;/p&gt;
&lt;div style=&#34;display: flex; align-items: center; background-color: #0f172a; border-radius: 8px; padding: 15px; border-left: 4px solid #3b82f6; color: #cbd5e1;&#34;&gt;
    &lt;div style=&#34;color: #3b82f6; font-size: 24px; margin-right: 12px; line-height: 1;&#34;&gt;&amp;#9432;&lt;/div&gt;
    &lt;div style=&#34;flex: 1; font-size: 16px;&#34;&gt;
        &lt;strong&gt;Note:&lt;/strong&gt; We can also use &lt;strong&gt;Async/Await&lt;/strong&gt; to solve the problem, but that&#39;s something new to learn.
    &lt;/div&gt;
&lt;/div&gt;
&amp;nbsp;
&lt;h2 id=&#34;summary&#34;&gt;Summary
&lt;/h2&gt;&lt;p&gt;In short, promise is a easer and more cleaner way to write asynchronous functions , avoiding callback hell , more maintainable code and other many advantages over class callback functions!&lt;/p&gt;
</description>
        </item>
        
    </channel>
</rss>
