<?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: Smriti</title>
    <description>The latest articles on DEV Community by Smriti (@smriti_4f19f53a3e55b45bcc).</description>
    <link>https://dev.to/smriti_4f19f53a3e55b45bcc</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%2F3265800%2Fca0e46a5-3219-4601-a45b-d49fe554c657.png</url>
      <title>DEV Community: Smriti</title>
      <link>https://dev.to/smriti_4f19f53a3e55b45bcc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smriti_4f19f53a3e55b45bcc"/>
    <language>en</language>
    <item>
      <title>JavaScript Promises: .all() vs .allSettled() and .race() vs .any()</title>
      <dc:creator>Smriti</dc:creator>
      <pubDate>Sat, 14 Jun 2025 21:08:05 +0000</pubDate>
      <link>https://dev.to/smriti_4f19f53a3e55b45bcc/javascript-promises-all-vs-allsettled-and-race-vs-any-5h7b</link>
      <guid>https://dev.to/smriti_4f19f53a3e55b45bcc/javascript-promises-all-vs-allsettled-and-race-vs-any-5h7b</guid>
      <description>&lt;p&gt;Managing multiple asynchronous operations in JavaScript can become complex. Fortunately, JavaScript provides several built-in methods—.all(), .allSettled(), .race(), and .any()—to handle such operations gracefully. This blog provides a professional, structured comparison of these promise methods with real code examples and practical use cases.&lt;br&gt;
&lt;strong&gt;Understanding the Basics&lt;/strong&gt;&lt;br&gt;
Before diving into comparisons, let’s revisit what a Promise is:&lt;br&gt;
A Promise is an object representing the eventual completion or failure of an asynchronous operation.&lt;br&gt;
Each of the methods we'll discuss below operates on an array of promises and provides different behaviors based on their resolution or rejection.&lt;br&gt;
&lt;strong&gt;Promise.all() – Wait for All to Succeed&lt;/strong&gt;&lt;br&gt;
Promise.all() returns a single promise that resolves when all of the input promises resolve, or rejects if any of the promises reject.&lt;br&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;&lt;br&gt;
Use when all tasks are required to succeed. If even one fails, the entire operation fails.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;&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 = Promise.resolve('Result 1');
const p2 = Promise.resolve('Result 2');
const p3 = Promise.reject('Error in p3');

Promise.all([p1, p2, p3])
  .then(results =&amp;gt; console.log('All success:', results))
  .catch(error =&amp;gt; console.error('At least one failed:', error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;At least one failed: Error in p3&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Promise.allSettled() – Wait for All to Finish (Regardless of Success or Failure)&lt;/strong&gt;&lt;br&gt;
Returns a promise that resolves after all promises have settled (fulfilled or rejected). It never rejects.&lt;br&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.allSettled([p1, p2, p3])
  .then(results =&amp;gt; console.log('All settled:', results));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;[&lt;br&gt;
  { status: 'fulfilled', value: 'Result 1' },&lt;br&gt;
  { status: 'fulfilled', value: 'Result 2' },&lt;br&gt;
  { status: 'rejected', reason: 'Error in p3' }&lt;br&gt;
]&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Promise.race() – First Settled Wins&lt;/strong&gt;&lt;br&gt;
Returns a promise that settles as soon as the first promise settles (either resolved or rejected).&lt;br&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;&lt;br&gt;
Use when you care about the fastest result, regardless of outcome.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fast = new Promise(resolve =&amp;gt; setTimeout(() =&amp;gt; resolve("Fast!"), 100));
const slow = new Promise(resolve =&amp;gt; setTimeout(() =&amp;gt; resolve("Slow!"), 500));

Promise.race([fast, slow])
  .then(result =&amp;gt; console.log('Winner:', result));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Winner: Fast!&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Promise.any() – First Successful Only&lt;/strong&gt;&lt;br&gt;
Returns a promise that fulfills as soon as any of the input promises fulfills. Rejects only if all of them reject.&lt;br&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;&lt;br&gt;
Use when you want at least one success, and can ignore failures&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fail1 = Promise.reject("Fail 1");
const fail2 = Promise.reject("Fail 2");
const success = Promise.resolve("Success!");

Promise.any([fail1, fail2, success])
  .then(result =&amp;gt; console.log("First success:", result))
  .catch(err =&amp;gt; console.log("All failed", err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;First success: Success!&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Promise.all() when everything must succeed.&lt;/li&gt;
&lt;li&gt;Use Promise.allSettled() to inspect all outcomes.&lt;/li&gt;
&lt;li&gt;Use Promise.race() for the fastest result (good or bad).&lt;/li&gt;
&lt;li&gt;Use Promise.any() to get the first successful result only.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>A Beginner's Guide to Promises in JavaScript (with Real Examples)</title>
      <dc:creator>Smriti</dc:creator>
      <pubDate>Sat, 14 Jun 2025 20:49:55 +0000</pubDate>
      <link>https://dev.to/smriti_4f19f53a3e55b45bcc/a-beginners-guide-to-promises-in-javascript-with-real-examples-6a4</link>
      <guid>https://dev.to/smriti_4f19f53a3e55b45bcc/a-beginners-guide-to-promises-in-javascript-with-real-examples-6a4</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a Promise?&lt;/strong&gt;&lt;br&gt;
A Promise is just a way for JavaScript to handle asynchronous operations - things that take time&lt;br&gt;
like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetching data from a server&lt;/li&gt;
&lt;li&gt;Reading a file&lt;/li&gt;
&lt;li&gt;Waiting for a timeout
Instead of blocking everything, JavaScript says:
"Hey! I'll do this task, and when I'm done, I'll let you know."
A Promise can be in 3 states:&lt;/li&gt;
&lt;li&gt;Pending - task is not done yet&lt;/li&gt;
&lt;li&gt;Resolved - task completed successfully&lt;/li&gt;
&lt;li&gt;Rejected - task failed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic Syntax of a Promise&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise = new Promise((resolve, reject) =&amp;gt; {
let success = true;
if (success) {
resolve("Task done!");
} else {
reject("Something went wrong.");
}
});
promise
.then((result) =&amp;gt; {
console.log(result);
})
.catch((error) =&amp;gt; {
console.log(error);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Real Example: Fake API Call&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fakeAPICall() {
return new Promise((resolve, reject) =&amp;gt; {
setTimeout(() =&amp;gt; {
resolve("Data received!");
}, 2000);
});
}
fakeAPICall()
.then(data =&amp;gt; console.log(data))
.catch(err =&amp;gt; console.log(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output after 2 seconds: Data received!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chaining Promises&lt;/strong&gt;&lt;br&gt;
doSomething()&lt;br&gt;
.then(result1 =&amp;gt; doSomethingElse(result1))&lt;br&gt;
.then(result2 =&amp;gt; doMore(result2))&lt;br&gt;
.catch(error =&amp;gt; console.log(error));&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Personal Tip&lt;/strong&gt;&lt;br&gt;
Think of them like a food delivery app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You order (async task starts)&lt;/li&gt;
&lt;li&gt;You get food (resolve)&lt;/li&gt;
&lt;li&gt;Or delivery fails (reject)
And .then() is what you do when food arrives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Resources That Helped Me&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MDN Web Docs - Promises&lt;/li&gt;
&lt;li&gt;JavaScript.info - Promises&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
At first, I used to avoid Promises. But once I started coding with real APIs in React, I had to learn&lt;br&gt;
them - and I'm glad I did.&lt;br&gt;
If you're new to Promises, I hope this helped you simplify the concept!&lt;br&gt;
Drop a comment if you found this helpful or if you want me to write about async/await next!&lt;br&gt;
Thanks for reading!&lt;br&gt;
Follow me on Dev.to - this is just the start of my blog journey&lt;/p&gt;

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