<?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>Forem: vcoder10</title>
    <description>The latest articles on Forem by vcoder10 (@vcoder10).</description>
    <link>https://forem.com/vcoder10</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%2F1231199%2F88d001c1-1fe0-4bd7-a423-79cd3d6c231e.jpeg</url>
      <title>Forem: vcoder10</title>
      <link>https://forem.com/vcoder10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vcoder10"/>
    <language>en</language>
    <item>
      <title>Polyfills of Promise.all()</title>
      <dc:creator>vcoder10</dc:creator>
      <pubDate>Tue, 22 Oct 2024 06:48:07 +0000</pubDate>
      <link>https://forem.com/vcoder10/polyfills-of-promiseall-2ogg</link>
      <guid>https://forem.com/vcoder10/polyfills-of-promiseall-2ogg</guid>
      <description>&lt;h2&gt;
  
  
  Function of Promise.all()
&lt;/h2&gt;

&lt;p&gt;Input : It takes array of Promise (not necessary)&lt;br&gt;
Output: It return Promise which contain array of result of all success promise.&lt;br&gt;
Note: If any promise fails, it reject immediately.&lt;/p&gt;

&lt;h2&gt;
  
  
  code for the Promise.myAll()
&lt;/h2&gt;

&lt;p&gt;`&lt;br&gt;
Promise.myAll = function (promises) {&lt;br&gt;
  return new Promise(function (resolve, reject) {&lt;br&gt;
    // Check if input is an array&lt;br&gt;
    if (!Array.isArray(promises)) {&lt;br&gt;
      return reject(new TypeError("Argument must be an array"));&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let results = [];
let completedPromises = 0;

promises.forEach(function (promise, index) {
  // Use Promise.resolve to handle non-promise values
  Promise.resolve(promise)
    .then(function (value) {
      results[index] = value;
      completedPromises += 1;

      // If all promises are resolved
      if (completedPromises === promises.length) {
        resolve(results);
      }
    })
    .catch(function (error) {
      reject(error); // Reject if any promise fails
    });
});

// Handle case with empty array of promises
if (promises.length === 0) {
  resolve([]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;br&gt;
};&lt;br&gt;
`&lt;/p&gt;

&lt;h2&gt;
  
  
  1. If all promise resolved successfully
&lt;/h2&gt;

&lt;p&gt;. All promises resolve, and their results are stored in the results array.&lt;br&gt;
. When the number of resolved promises equals the length of the input array, the outer promise resolves with the results array.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Why to use Promise.resolve()
&lt;/h2&gt;

&lt;p&gt;. It is not neccessay that in array of promises all value must be a promise, it can by any value - number, string, or any synchronus function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If the passed value is already a promise - It will return the same promise, ensuring that no extra wrapping or changing occur.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the passed value is not a promise - It will wrap the value in resolved promise, allowing to treat it like a promise and handle it with .then()&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Why Promise.resolve(), why not Promise.reject()
&lt;/h2&gt;

&lt;p&gt;so that it resolve with value, if we use reject it will caught in catch block with value considered as error.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. What happend if any Promise fails?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;This trigger the reject() call of the outer promie causing the Promise.myAll() to reject immediately.&lt;/li&gt;
&lt;li&gt;foreach loop will continue, as it synchronus program&lt;/li&gt;
&lt;li&gt;rest of promise will resolve and its valued added to result but as completedPromise!=promises.length it not resolve, and if still try it will be ignored because Promise.myAll() is not in pending state now.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you still have any questions, feel free to ask in the comments!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
