<?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: Rakesh Joshi</title>
    <description>The latest articles on DEV Community by Rakesh Joshi (@joshirakesh165).</description>
    <link>https://dev.to/joshirakesh165</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%2F1393430%2Fe559c9b1-a149-4411-be7c-428eea8d321a.jpeg</url>
      <title>DEV Community: Rakesh Joshi</title>
      <link>https://dev.to/joshirakesh165</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshirakesh165"/>
    <language>en</language>
    <item>
      <title>Custom Promise.all</title>
      <dc:creator>Rakesh Joshi</dc:creator>
      <pubDate>Sun, 07 Apr 2024 10:02:26 +0000</pubDate>
      <link>https://dev.to/joshirakesh165/custom-promiseall-17dh</link>
      <guid>https://dev.to/joshirakesh165/custom-promiseall-17dh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Promise.all()&lt;/strong&gt; is a method in JavaScript that takes an array of promises and returns a single Promise that resolves when all of the promises in the array have resolved, or rejects when any of the input's promises rejects, with this first rejection reason.&lt;/p&gt;

&lt;p&gt;Here's a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const p1 = new Promise(function (resolve, reject) {
    setTimeout(() =&amp;gt; {
        resolve("result 1")
    }, 3000)
})

const p2 = new Promise(function (resolve, reject) {
    setTimeout(() =&amp;gt; {
        resolve("result 2")
    }, 1000)
})

const p3 = new Promise(function (resolve, reject) {
    setTimeout(() =&amp;gt; {
        resolve("result 3")
    }, 0)
})


Promise.all([p1, p2, p3])
    .then(response =&amp;gt; console.log('Response is ', response))
    .catch(e =&amp;gt; console.log(e));

// output -    Response is ['result 1', 'result 2', 'result 3']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;strong&gt;Promise.all()&lt;/strong&gt; waits for all three promises to resolve, and then it resolves itself with an array containing the resolved values of all three promises. If any of the promises are rejected, the Promise.all() call immediately rejects with the reason of the first rejected promise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom implementation for Promise.all
&lt;/h2&gt;

&lt;p&gt;let's start creating our custom promise.all functions lets name it as  &lt;strong&gt;customAll&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we discussed in starting, &lt;strong&gt;promise.all&lt;/strong&gt; accept an array and return a promise so we will start with this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.customAll = function (promiseList) {
        return new Promise(resolve, reject){
             // code ... 
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we will create an array named &lt;strong&gt;results&lt;/strong&gt; for storing response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.customAll = function (promiseList) {
            let results = [];
            return new Promise(resolve, reject) {
               // code ... 
            }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;step 3&lt;/strong&gt;&lt;br&gt;
We will iterate input array and resolve the promise using &lt;strong&gt;Promise.resolve()&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; - &lt;em&gt;we are manually resolving input using &lt;strong&gt;Promise.resolve()&lt;/strong&gt; because &lt;strong&gt;Promise.all&lt;/strong&gt; can also accept normal values as input not necessarily promise. &lt;br&gt;
In case we will not manually resolve, it will throw an error ".then is not a function" for non promise values&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here we will consider three cases&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If promise is resolved we will store the result in &lt;strong&gt;results&lt;/strong&gt; array with respective index and increment a count variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If promise will be rejected we will reject the returning promise with error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If all the promises are passed in this case &lt;strong&gt;count&lt;/strong&gt; will be equal to  &lt;strong&gt;promiseList.length&lt;/strong&gt; (input), and we will resolve the returning promise.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In code we have only two cases one for success and other for error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.customAll =  function (promiseList) {
    let results = [];
    let count = 0;
    return new Promise((resolve, reject) =&amp;gt; {

        promiseList.forEach((promsie,index) =&amp;gt; {

            Promise.resolve(promsie)
            .then(response =&amp;gt; {
                results[index] = response;
                count++;
                if (count == promiseList.length) {
                    resolve(results);
                }
            }) 
            .catch(error =&amp;gt; {
                reject(error);
            })

        })

    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its time to test are &lt;strong&gt;customAll&lt;/strong&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.customAll([p1, p2, p3])
    .then(response =&amp;gt; console.log('Response is ', response))
    .catch(e =&amp;gt; console.log(e));

// output -    Response is ['result 1', 'result 2', 'result 3']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can test this code for error cases.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
