<?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: Saurav Gupta</title>
    <description>The latest articles on DEV Community by Saurav Gupta (@sauravgupta2800).</description>
    <link>https://dev.to/sauravgupta2800</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%2F617885%2F6767e65a-4e38-4838-9ada-5f59ef3b5276.jpeg</url>
      <title>DEV Community: Saurav Gupta</title>
      <link>https://dev.to/sauravgupta2800</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sauravgupta2800"/>
    <language>en</language>
    <item>
      <title>Implement custom Promise.all()</title>
      <dc:creator>Saurav Gupta</dc:creator>
      <pubDate>Thu, 05 Aug 2021 05:47:46 +0000</pubDate>
      <link>https://dev.to/sauravgupta2800/implement-custom-promise-all-g7g</link>
      <guid>https://dev.to/sauravgupta2800/implement-custom-promise-all-g7g</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.myAll = function (values) {
  return new Promise((resolve, reject) =&amp;gt; {
    let results = [];
    let completed = 0;
    values.forEach((singlePromise, index) =&amp;gt; {
      singlePromise
        .then((res) =&amp;gt; {
          results[index] = res;
          completed++;
          if (completed === values.length) resolve(results);
        })
        .catch((e) =&amp;gt; {
          reject(e);
        });
    });
  });
};

function task(time) {
  return new Promise((resolve, reject) =&amp;gt; {
    setTimeout(() =&amp;gt; resolve(time), time);
  });
}

const taskList = [task(1000), task(5000), task(3000)];

Promise.myAll(taskList)
  .then((results) =&amp;gt; {
    console.log("got results", results);
  })
  .catch(console.error);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Different ways to Flatten the array in JavaScript</title>
      <dc:creator>Saurav Gupta</dc:creator>
      <pubDate>Wed, 04 Aug 2021 09:05:19 +0000</pubDate>
      <link>https://dev.to/sauravgupta2800/different-ways-to-flatten-the-array-in-javascript-8a8</link>
      <guid>https://dev.to/sauravgupta2800/different-ways-to-flatten-the-array-in-javascript-8a8</guid>
      <description>&lt;h1&gt;
  
  
  Using Reduce
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function flatten(arr) {
  return arr.reduce((acc, item) =&amp;gt; {
    if (Array.isArray(item)) {
      acc = [...acc, ...flatten(item)];
    } else {
      acc.push(item);
    }
    return acc;
  }, []);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Using Recursion
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function flattenRecursion(arr) {
  if (!arr.length) return [];
  let res = [];
  for (let i = 0; i &amp;lt; arr.length; i++) {
    const item = arr[i];
    if (Array.isArray(item)) {
      res = [...res, ...flattenRecursion(item)];
    } else {
      res.push(item);
    }
  }
  return res;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
