🟢 EasyAsync JS📖 Theory Question

What are Promise combinators and when do you use each?

💡

Hint

all=all resolve, allSettled=wait all, race=first settles, any=first resolves

Full Answer

  • Promise.all() — waits for ALL to resolve. Rejects immediately if ANY rejects. Use when all must succeed.
  • Promise.allSettled() — waits for ALL to settle (resolve OR reject). Never rejects itself. Use when you need all results regardless of failure.
  • Promise.race() — settles with the FIRST settled promise (resolve or reject). Use for timeout patterns.
  • Promise.any() — resolves with the FIRST resolved promise. Rejects only if ALL reject. Use for fallback/redundancy.
const fast = fetch('/fast');   // 100ms
const slow = fetch('/slow');   // 500ms
const bad  = fetch('/broken'); // 200ms, rejects

await Promise.all([fast, slow]);    // ✅ 500ms (waits for both)
await Promise.all([fast, bad]);     // ❌ rejects at 200ms

const results = await Promise.allSettled([fast, slow, bad]);
// [{status:'fulfilled', value:...}, ..., {status:'rejected', reason:...}]

await Promise.race([fast, slow]);   // resolves at 100ms with fast result

await Promise.any([bad, fast]);     // resolves at 100ms (ignores bad)

// Timeout pattern with race:
const withTimeout = (p, ms) => Promise.race([
  p,
  new Promise((_, r) => setTimeout(() => r(new Error('Timeout')), ms))
]);
💡 allSettled is your safety net — it always resolves, making it great for "fire multiple requests, report all results" patterns.

More Async JS Questions

🟢 EasyExplain Promises — states, chaining, and error handling.🟢 EasyHow does async/await work under the hood?🟢 EasyWhat is callback hell and how do you avoid it?🟢 EasyWhat is queueMicrotask() and when should you use it?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint