Promise Combinators
Use all, allSettled, race, and any according to failure and completion requirements.
Lesson content
🎯 Goal By the end of this lesson, you'll understand: What are Promise Combinators? Promise.all() Promise.allSettled() Promise.race() Promise.any() When to use each Interview questions What are Promise Combinators? Promise Combinators are methods that work with multiple Promises at the same time . Simple Definition: They help you manage multiple asynchronous tasks together. Real-Life Example 🚚 Imagine ordering three products online. Laptop Phone Headphones Instead of tracking each separately, Promise Combinators manage them together. 1. Promise.all() Waits for all Promises to complete successfully. If one Promise fails , the entire Promise fails. Example const p1 = Promise . resolve( "Apple" ); const p2 = Promise . resolve( "Banana" ); const p3 = Promise . resolve( "Mango" ); Promise . all([ p1 , p2 , p3 ]) . then( result => console . log( result )); Output ["Apple", "Banana", "Mango"] If One Promise Fails Promise . all([ Promise . resolve( "A" ), Promise . reject( "Error" ), Promise . resolve( "C" ) ]) . catch( console . log); Output Error 2. Promise.allSettled() Waits for all Promises , whether they succeed or fail. Example Promise . allSettled([ Promise . resolve( "Success" ), Promise . reject( "Failed" ) ]) . then( console . log); Output [ { status: "fulfilled", value: "Success" }, { status: "rejected", reason: "Failed" } ] 3. Promise.race() Returns the first Promise that settles (fulfilled or rejected). Example const p1 = new Promise ( resolve => setTimeout (() => resolve ( "First" ), 1000 ) ); const p2 = new Promise ( resolve => setTimeout (() => resolve ( "Second" ), 2000 ) ); Promise . race([ p1 , p2 ]) . then( console . log); Output First 4. Promise.any() Returns the first successful Promise . Rejected Promises are ignored unless all Promises fail. Example Promise . any([ Promise . reject( "Error" ), Promise . resolve( "Success" ), Promise . resolve( "Done" ) ]) . then( console . log); Output Success Comparison Table Method Waits For If One Fails Promise.all() All success ❌ Rejects immediately Promise.allSettled() All finish ✅ Returns all results Promise.race() First settled Returns first success or failure Promise.any() First success Ignores failures unless all fail Real-Life Example 🛒 Order 3 Products │ ▼ Promise.all() → Wait for all deliveries Promise.allSettled() → Show delivered and failed items Promise.race() → First package arrives Promise.any() → First successful delivery When to Use Which? Situation Best Choice Need all API responses Promise.all() Need every result (success or failure) Promise.allSettled() Need the fastest response Promise.race() Need the first successful response Promise.any() Interview Question What is the output? Promise . all([ Promise . resolve( 1 ), Promise . resolve( 2 ), Promise . resolve( 3 ) ]) . then( console . log); Output [1, 2, 3] Key Points ✅ Promise.all() waits for all Promises to succeed. ✅ Promise.allSettled() returns every result, whether fulfilled or rejected. ✅ Promise.race() returns the first settled Promise. ✅ Promise.any() returns the first fulfilled Promise. ✅ Use the right combinator based on your application's needs. 🧠 Easy Formula Need ALL Success? │ ▼ Promise.all() Need ALL Results? │ ▼ Promise.allSettled() Need FASTEST Result? │ ▼ Promise.race() Need FIRST Success? │ ▼ Promise.any() Remember: ✅ Promise.all() → All must succeed. 📋 Promise.allSettled() → Get every result. 🏁 Promise.race() → First promise wins. 🎯 Promise.any() → First successful promise wins.