<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Javascript Promise.all]]></title><description><![CDATA[Javascript Promise.all]]></description><link>https://pv-kiran.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 06:30:02 GMT</lastBuildDate><atom:link href="https://pv-kiran.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding Promise.all : Running Multiple Promises at Once]]></title><description><![CDATA[Promise.all is a JavaScript method that lets you run multiple promises at the same time, instead of one after another. It takes an array of promises and returns a new promise that resolves when all the promises in the array have completed.
The Proble...]]></description><link>https://pv-kiran.hashnode.dev/understanding-promiseall-running-multiple-promises-at-once</link><guid isPermaLink="true">https://pv-kiran.hashnode.dev/understanding-promiseall-running-multiple-promises-at-once</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[promise methods]]></category><category><![CDATA[promise.all]]></category><category><![CDATA[performance]]></category><category><![CDATA[React]]></category><category><![CDATA[MongoDB]]></category><dc:creator><![CDATA[Kiran Pv]]></dc:creator><pubDate>Fri, 09 May 2025 05:16:27 GMT</pubDate><content:encoded><![CDATA[<p>Promise.all is a JavaScript method that lets you run multiple promises at the same time, instead of one after another. It takes an array of promises and returns a new promise that resolves when all the promises in the array have completed.</p>
<h2 id="heading-the-problem-promiseall-solves">The Problem Promise.all Solves</h2>
<p>When you're building apps, you often need to get data from different sources. For example, you might need to:</p>
<ul>
<li><p>Get menu items from restaurants</p>
</li>
<li><p>Check availability of rides</p>
</li>
<li><p>See what entertainment options are available</p>
</li>
</ul>
<p>Without Promise.all, you'd have to wait for each request to finish before starting the next one:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getEverything</span>(<span class="hljs-params">city</span>) </span>{
  <span class="hljs-keyword">const</span> restaurants = <span class="hljs-keyword">await</span> Restaurant.find({ <span class="hljs-attr">city</span>: city });
  <span class="hljs-keyword">const</span> homelyFood = <span class="hljs-keyword">await</span> HomelyFood.find({ <span class="hljs-attr">city</span>: city });
  <span class="hljs-keyword">const</span> rides = <span class="hljs-keyword">await</span> Rides.find();
  <span class="hljs-keyword">const</span> otherServices = <span class="hljs-keyword">await</span> OtherService.find();
  <span class="hljs-keyword">const</span> entertainment = <span class="hljs-keyword">await</span> Entertainment.find();


  <span class="hljs-keyword">return</span> {
    restaurants,
    homelyFood,
    rides,
    otherServices,
    entertainment,
    roomService
  };
}
</code></pre>
<p>This is slow because each request has to wait for the previous one to finish - even though they don't depend on each other!</p>
<h2 id="heading-how-promiseall-makes-things-better">How Promise.all Makes Things Better</h2>
<p>Promise.all runs all these tasks at the same time (in parallel), making our app much faster:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getEverything</span>(<span class="hljs-params">city</span>) </span>{
  <span class="hljs-keyword">const</span> [restaurants, homelyFood, rides, otherServices, entertainment, roomService] = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all([
    Restaurant.find({ <span class="hljs-attr">city</span>: city }),
    HomelyFood.find({ <span class="hljs-attr">city</span>: city }),
    Rides.find(),
    OtherService.find(),
    Entertainment.find(),
    RoomService.find()
  ]);

  <span class="hljs-keyword">return</span> {
    restaurants,
    homelyFood,
    rides,
    otherServices,
    entertainment, 
    roomService
  };
}
</code></pre>
<h2 id="heading-comparing-the-speed-difference">Comparing the Speed Difference</h2>
<p>Let's say each request takes about 1 second:</p>
<ul>
<li><p>Without Promise.all: 6 seconds total (1 + 1 + 1 + 1 + 1 + 1)</p>
</li>
<li><p>With Promise.all: Only about 1 second total (all requests happen at once!)</p>
</li>
</ul>
<h2 id="heading-when-to-use-promiseall">When to Use Promise.all</h2>
<p>Use Promise.all when:</p>
<ul>
<li><p>You need to make multiple async calls</p>
</li>
<li><p>The calls don't depend on each other's results</p>
</li>
<li><p>You need all the results before moving forward</p>
</li>
</ul>
<h2 id="heading-when-not-to-use-promiseall">When Not to Use Promise.all</h2>
<p>Don't use Promise.all when:</p>
<ul>
<li><p>Tasks need to happen one after another</p>
</li>
<li><p>You need the result from one task before starting another</p>
</li>
<li><p>You want to continue even if some tasks fail (in this case, consider Promise.allSettled instead)</p>
</li>
</ul>
<h2 id="heading-how-promiseall-handles-errors">How Promise.all Handles Errors</h2>
<p>If any promise in the array fails (rejects), the entire Promise.all will fail immediately. It's like a chain - one break ruins the whole thing.</p>
<p>For example, if the Restaurant.find() call fails but all others succeed, your code will jump to the catch block without waiting for the other promises to finish.</p>
<h2 id="heading-real-world-example-explained">Real-world Example Explained</h2>
<p>Let's break down our code example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [restaurants, homelyFood, rides, otherServices, entertainment, roomService] = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all([
  Restaurant.find({ <span class="hljs-attr">city</span>: city }),
  HomelyFood.find({ <span class="hljs-attr">city</span>: city }),
  Rides.find(),
  OtherService.find(),
  Entertainment.find(),
  RoomService.find()
]);
</code></pre>
<p>This code:</p>
<ol>
<li><p>Creates an array with 6 database queries</p>
</li>
<li><p>Runs all 6 queries at the same time using Promise.all</p>
</li>
<li><p>Waits for all queries to finish with the await keyword</p>
</li>
<li><p>Uses array destructuring to assign each result to a separate variable</p>
</li>
<li><p>Continues running your code only after all data is available</p>
</li>
</ol>
<h2 id="heading-summary">Summary</h2>
<p>Promise.all is a powerful tool that makes your code:</p>
<ul>
<li><p>Faster by running tasks in parallel</p>
</li>
<li><p>Cleaner and easier to read</p>
</li>
<li><p>More efficient with resources</p>
</li>
</ul>
<p>Instead of waiting for tasks one by one like standing in a long line, Promise.all lets tasks run simultaneously like multiple checkout lanes at a store - getting everything done much faster!</p>
]]></content:encoded></item></channel></rss>