<?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: Alaa Ezz Eldin</title>
    <description>The latest articles on DEV Community by Alaa Ezz Eldin (@alaa_ezzeldin).</description>
    <link>https://dev.to/alaa_ezzeldin</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%2F1949280%2Ff7b5fb05-ad2b-49ac-a0cb-70c5e5221ab5.jpg</url>
      <title>DEV Community: Alaa Ezz Eldin</title>
      <link>https://dev.to/alaa_ezzeldin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alaa_ezzeldin"/>
    <language>en</language>
    <item>
      <title>Async Loops in JavaScript: for...of vs forEach</title>
      <dc:creator>Alaa Ezz Eldin</dc:creator>
      <pubDate>Wed, 28 Aug 2024 21:08:53 +0000</pubDate>
      <link>https://dev.to/alaa_ezzeldin/async-loops-in-javascript-forof-vs-foreach-5g2m</link>
      <guid>https://dev.to/alaa_ezzeldin/async-loops-in-javascript-forof-vs-foreach-5g2m</guid>
      <description>&lt;p&gt;JavaScript’s async capabilities are pretty cool 😎, but choosing the right loop to handle those async tasks can make a big difference. Let’s break down the difference between &lt;code&gt;for...of&lt;/code&gt; and &lt;code&gt;forEach&lt;/code&gt; with a splash of fun 🎉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. for...of Loop with Async Functions&lt;/strong&gt;&lt;br&gt;
Imagine the &lt;code&gt;for...of&lt;/code&gt; loop as your super-diligent friend who waits patiently for you to finish one task before starting the next. It’s like waiting for your coffee to brew before you start your next task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (const item of items) {
  await doSomethingAsync(item);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose you have an array of tasks that each return a resolved promise with a delay:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doSomethingAsync(item) {
  return new Promise((resolve) =&amp;gt; {
    setTimeout(() =&amp;gt; resolve(`Processed ${item}`), 1000);
  });
}

async function processItemsSequentially(items) {
  for (const item of items) {
    try {
      const result = await doSomethingAsync(item);
      console.log(result);
    } catch (error) {
      console.error(`Oops! Error with ${item}:`, error);
    }
  }
}

const items = ['Task 1', 'Task 2', 'Task 3'];
processItemsSequentially(items);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;processItemsSequentially&lt;/code&gt; waits for each &lt;code&gt;doSomethingAsync&lt;/code&gt; call to complete before moving on to the next item.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why You’ll Love It:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🕰️ Patience: It waits for each async task to finish before moving on. It’s all about that orderly life.&lt;/p&gt;

&lt;p&gt;🚨 Error Handling: You can easily catch and handle errors within the loop.&lt;/p&gt;

&lt;p&gt;🔥 Perfect For: When you need things done in order – like cooking a meal where you can’t bake the cake before mixing the batter XD.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;2. forEach with Async Functions&lt;/strong&gt;&lt;br&gt;
On the flip side, &lt;code&gt;forEach&lt;/code&gt; is like a bunch of friends who all start their tasks at the same time. They’re excited and want to finish as quickly as possible. They don’t care if one person finishes before the others; they just go for it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;items.forEach(async (item) =&amp;gt; {
  await doSomethingAsync(item);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s use the same &lt;code&gt;doSomethingAsync&lt;/code&gt; function but with &lt;code&gt;forEach&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function processItemsConcurrently(items) {
  items.forEach(async (item) =&amp;gt; {
    try {
      const result = await doSomethingAsync(item);
      console.log(result);
    } catch (error) {
      console.error(`Whoops! Issue with ${item}:`, error);
    }
  });
}

const items = ['Task A', 'Task B', 'Task C'];
processItemsConcurrently(items);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;processItemsConcurrently&lt;/code&gt; starts all the &lt;code&gt;doSomethingAsync&lt;/code&gt; calls simultaneously, so all tasks run in parallel.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why It’s Fun:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🏃‍♂️🏃‍♀️ Parallel Execution: All tasks start at once, which can be super fast but a bit chaotic.&lt;/p&gt;

&lt;p&gt;✂️ Simpler Syntax: Often shorter and more concise than for...of.&lt;/p&gt;

&lt;p&gt;🔥 Perfect For: When tasks don’t depend on each other and can be performed independently, like fetching data from multiple APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;for...of&lt;/code&gt; is your orderly friend who waits their turn.&lt;br&gt;
&lt;code&gt;forEach&lt;/code&gt; is all about jumping in at the same time and getting things done fast.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for...of&lt;/code&gt; lets you catch errors one by one, which is neat and tidy.&lt;br&gt;
&lt;code&gt;forEach&lt;/code&gt; makes you handle errors on the fly, like juggling multiple balls.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for...of&lt;/code&gt; might be a tad slower, but it’s neat and orderly.&lt;br&gt;
&lt;code&gt;forEach&lt;/code&gt; can be faster if you’re cool with the chaos and tasks don’t overlap.&lt;/p&gt;

&lt;p&gt;Choose the loop that fits your needs based on whether you need order and patience or speed and excitement! 🚀🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>loops</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
