<?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: Mahesh Konne</title>
    <description>The latest articles on DEV Community by Mahesh Konne (@mr_7).</description>
    <link>https://dev.to/mr_7</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%2F134610%2Fc3cd554a-ab98-4929-a18e-6e15fa55bfaa.jpeg</url>
      <title>DEV Community: Mahesh Konne</title>
      <link>https://dev.to/mr_7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mr_7"/>
    <language>en</language>
    <item>
      <title>Effective way to solve Promise Concurrency in Javascript</title>
      <dc:creator>Mahesh Konne</dc:creator>
      <pubDate>Sun, 22 Jan 2023 10:28:24 +0000</pubDate>
      <link>https://dev.to/mr_7/effective-way-to-solve-promise-concurrency-in-javascript-4emd</link>
      <guid>https://dev.to/mr_7/effective-way-to-solve-promise-concurrency-in-javascript-4emd</guid>
      <description>&lt;p&gt;As a web developer, sometimes I ran into situations where I need to make concurrent API calls and my go to solution was &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" rel="noopener noreferrer"&gt;Promise.all()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;yes, It solves the problem. But most of the developers do not know(or don't care?) about the problem it comes with.. "Error handling"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Okay, What's the Problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.all()&lt;/code&gt; takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises fulfill, with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason.&lt;/p&gt;

&lt;p&gt;Here's an example code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = await fetchUser();
const posts = await fetchPosts();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;above two API's are independent to each other. And to optimize the performance, we can call them concurrently&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome. It looks good now. The next step we need to take care of while working with Asynchronous code is "Error Handling"&lt;/p&gt;

&lt;p&gt;let's catch the errors..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
    const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]);
} catch (e) {
    // Handle the error
    console.log(`Something went wrong: ${e}`)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;even though it looks good, there's a big problem here. The returned promise rejects when any of the input's promises rejects.. meaning If both the API calls fail,  we can only be able to catch the first rejection reason here..&lt;/p&gt;

&lt;p&gt;how to fix this? 🤔&lt;/p&gt;

&lt;p&gt;The answer is &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled" rel="noopener noreferrer"&gt;Promise.allSettled()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Promise.allSettled() takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises settle, with an array of objects that describe the outcome of each promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [user, posts] = await Promise.allSettled([fetchUser(), fetchPosts()]);

if(user.status === "rejected") {
    // handle user error
}

if(posts.status === "rejected") {
    // handle posts error
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bang!🎉 It perfectly serves our needs.&lt;/p&gt;

&lt;p&gt;Here are the key takeways 📝:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Promise.all()&lt;/code&gt; will reject as soon as one of the Promises in the array rejects.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.allSettled()&lt;/code&gt; will resolve once all Promises in the array have either resolved or rejected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this useful, don't forget to follow me for more JS stuff. Cheers! 🥂&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promise</category>
      <category>errors</category>
      <category>allsettled</category>
    </item>
  </channel>
</rss>
