<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
>

<channel>
	<title>async - Bipon's Diary</title>
	<atom:link href="https://biponnotes.iglyphic.com/tag/async/feed/" rel="self" type="application/rss+xml" />
	<link>https://biponnotes.iglyphic.com</link>
	<description>Do good for others. It will come back in unexpected ways.</description>
	<lastBuildDate>Tue, 28 Jan 2020 10:02:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.9.10</generator>
<site xmlns="com-wordpress:feed-additions:1">168324471</site>	<item>
		<title>JavaScript: async and await</title>
		<link>https://biponnotes.iglyphic.com/javascript-async-and-await/</link>
					<comments>https://biponnotes.iglyphic.com/javascript-async-and-await/#respond</comments>
		
		<dc:creator><![CDATA[bipon68]]></dc:creator>
		<pubDate>Tue, 28 Jan 2020 10:02:27 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[await]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://bipon.me/?p=469</guid>

					<description><![CDATA[<p>If the arrow function is: This is how you can identify asynchronous code in your function. This time you have to identify the original asynchronous codes one by one and tell Javascript that this is my promise that it cannot go to the next instruction until it is solved. Before that we will need a promiss, right? Yes,&#8230;<a href="https://biponnotes.iglyphic.com/javascript-async-and-await/" class="more-link">Continue reading <span class="screen-reader-text">JavaScript: async and await</span></a></p>
<p>The post <a href="https://biponnotes.iglyphic.com/javascript-async-and-await/">JavaScript: async and await</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></description>
										<content:encoded><![CDATA[<pre class="wp-block-code"><code>async function customAsyncCode() {
   // Asynchronous Codes
}  </code></pre>



<p> If the arrow function is: </p>



<pre class="wp-block-code"><code> const customAsyncCode = async() => {      
     // Asynchronous Codes    
} </code></pre>



<p> This is how you can identify asynchronous code in your function. This time you have to identify the original asynchronous codes one by one and tell Javascript that this is my promise that it cannot go to the next instruction until it is solved. Before that we will need a promiss, right? Yes, let me start with a promise: </p>



<pre class="wp-block-code"><code>      const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })</code></pre>



<p> Now we will handle this promise inside an asynchronous function. That is why I will call our Promise talk inside with an asynchronous identifying function. Now this is where we <code>await</code>need the keyword. With this we will identify our Promise so that this Promise does not go to the next instruction until completed: </p>



<pre class="wp-block-code"><code>const promiseHandle = async() => {
        const data = await a1;
        console.log(data);
      }
      promiseHandle();</code></pre>



<p> Notice here that I just printed the data from our premise in the next line. Yes, here <code>async</code>is <code>await</code>the magic of it. It helps our asynchronous code behave asynchronously so that we no longer have to call the callback. We can retrieve data with a single line-by-line instruction. Now if you call this function you will see our desired data: </p>



<pre class="wp-block-code"><code>promiseHandle()</code></pre>



<p> Now you will see that your console is printed on the next line of console log. If you have trouble understanding it, then <code>setTimeout()</code>you can extend the time and test it yourself by working code synchronously: </p>



<figure class="wp-block-image"><img width="486" height="182" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/1-5.png?resize=486%2C182&#038;ssl=1" alt="" class="wp-image-470" srcset="https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-5.png?w=486&amp;ssl=1 486w, https://i2.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/1-5.png?resize=300%2C112&amp;ssl=1 300w" sizes="(max-width: 486px) 100vw, 486px" data-recalc-dims="1" /></figure>



<p> Now if the promise is rejected? Yes then <code>try…catch</code>we can handle this rejection or error with our previous block. Before that, our promise to create another such thing: </p>



<pre class="wp-block-code"><code>      const a1Rejection = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            reject('a 1 error');
          }else{
            resolve('a 1 resolved');
          }
        }, 3000)
      })</code></pre>



<p> Now we <code>async await</code>will create our function. But before we can take data from this promise, we have to <code>try…catch</code>put the whole thing inside the block. And with that <code>catch</code>block we can access our error message: </p>



<pre class="wp-block-code"><code>     const promiseErrorHandle = async() => {
        try{
          const data = await a1Rejection;
          console.log(data);
        }catch(err){
          console.log(err);
        }
      }
</code></pre>



<pre class="wp-block-code"><code>promiseErrorHandle();</code></pre>



<p><strong>An alternative method to handle more than one promise</strong> , now we want to promise more than they can handle  synchronously. Suppose we have two promise: </p>



<pre class="wp-block-code"><code>   const a1 = new Promise((resolve, reject) => {
        setTimeout(() => {
          if(true){
            resolve('a 1 resolved');
          }else{
            reject('a 1 error');
          }
        }, 3000)
      })</code></pre>



<p>

Another one

</p>



<pre class="wp-block-code"><code>     const a2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            if(true) {
              resolve('a 2 Resolved');
            } else {
              reject('a 2 Error');
            }
        }, 3000)
      })</code></pre>



<p> Now here&#8217;s something to understand. If we <code>Promise.all</code>use it, let&#8217;s see if it returns: </p>



<pre class="wp-block-code"><code>console.log('Return all Promise :', Promise.all([a1, a2]))</code></pre>



<p> That means the promise also returns: </p>



<figure class="wp-block-image"><img loading="lazy" width="571" height="146" src="https://i0.wp.com/bipon.me/wp-content/uploads/2020/01/2-2.png?resize=571%2C146&#038;ssl=1" alt="" class="wp-image-471" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-2.png?w=571&amp;ssl=1 571w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/2-2.png?resize=300%2C77&amp;ssl=1 300w" sizes="(max-width: 571px) 100vw, 571px" data-recalc-dims="1" /></figure>



<p> So with that we can easily handle multiple promises like this: </p>



<pre class="wp-block-code"><code> const handleMultiplePromise = async() => {
        const data = await Promise.all([a1, a2]);
        console.log(data);
}</code></pre>



<p> Well then how come their data? Yes the data will come in the same array size as we have seen before. Now call the function: </p>



<pre class="wp-block-code"><code>handleMultiplePromise();
</code></pre>



<figure class="wp-block-image"><img loading="lazy" width="568" height="182" src="https://i2.wp.com/bipon.me/wp-content/uploads/2020/01/3-3.png?resize=568%2C182&#038;ssl=1" alt="" class="wp-image-472" srcset="https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-3.png?w=568&amp;ssl=1 568w, https://i0.wp.com/biponnotes.iglyphic.com/wp-content/uploads/2020/01/3-3.png?resize=300%2C96&amp;ssl=1 300w" sizes="(max-width: 568px) 100vw, 568px" data-recalc-dims="1" /></figure><p>The post <a href="https://biponnotes.iglyphic.com/javascript-async-and-await/">JavaScript: async and await</a> first appeared on <a href="https://biponnotes.iglyphic.com">Bipon's Diary</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://biponnotes.iglyphic.com/javascript-async-and-await/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">469</post-id>	</item>
	</channel>
</rss>
