Mastering Promise Patterns in JavaScript: all, allSettled, and race

Many developers use Promises daily… but few clearly understand the difference between these three: - `Promise.all()` - `Promise.allSettled()` - `Promise.race()` They look similar - but their behavior is completely different. Here’s the simple way to remember them 1️⃣ `Promise.all()` Runs promises in parallel and waits for all of them to succeed. If one fails everything fails immediately. ``` await Promise.all([p1, p2, p3]) ``` Use when every result is required Example: loading user profile, posts, and followers. 2️⃣ `Promise.allSettled()` Waits for all promises to finish, whether they succeed or fail. ``` await Promise.allSettled([p1, p2, p3]) ``` Result looks like: ``` [ {status: "fulfilled", value: ...}, {status: "rejected", reason: ...} ] ``` Use when you want all results, even if some fail. Perfect for dashboards or batch operations. 3️⃣ `Promise.race()` Returns the first promise that settles (resolve OR reject). ``` await Promise.race([p1, p2, p3]) ``` Commonly used for timeouts. Example: API request vs timeout timer. Easy way to remember `Promise.all` - All must succeed `Promise.allSettled` - Give me every result `Promise.race` -Whoever finishes first wins Mastering these small patterns can significantly improve performance and reliability in async JavaScript. Most developers learn Promises… But strong engineers learn how to orchestrate them. #javascript #webdevelopment #softwareengineering #frontend #nodejs

To view or add a comment, sign in

Explore content categories