Course contentsShow
← JavaScript
Lesson 1371 of 2,72330. Async/AwaitFree lesson

Promise.allSettled for Resilient Parallel Execution

Use Promise.allSettled to run operations in parallel and handle each result independently, even if some fail.

Promise.allSettled for Resilient Parallel Execution

What you'll learn: How to run multiple operations in parallel and handle each result individually, even when some fail.

The Problem with Promise.all

You've learned that Promise.all executes operations in parallel, but it has a critical weakness: if any promise rejects, the entire operation short-circuits. Sometimes you need a more resilient approach where one failure doesn't prevent you from processing the successes.

Using Promise.allSettled for Resilience

Promise.allSettled waits for all promises to complete (settle) regardless of whether they fulfill or reject. It returns an array of result objects, each describing the outcome:

async function fetchMultipleResources() {
  const operations = [
    fetch('/api/users'),
    fetch('/api/invalid-endpoint'),
    fetch('/api/posts')
  ];

  const results = await Promise.allSettled(operations);

  results.forEach((result, index) => {
    if (result.status === 'fulfilled') {
      console.log(`Operation ${index} succeeded:`, result.value);
    } else {
      console.log(`Operation ${index} failed:`, result.reason);
    }
  });
}

When to Use This Pattern

Use Promise.allSettled when:

  • Operations are independent (one failure shouldn't stop others)
  • You want to collect all outcomes for reporting or partial success
  • You're implementing best-effort parallel operations

Think of it like sending multiple emails: if one address is invalid, you still want to send to the others and report which ones failed.

Extracting What You Need

async function getBestAvailableData() {
  const results = await Promise.allSettled([
    fetchPrimaryAPI(),
    fetchBackupAPI(),
    fetchCacheData()
  ]);

  // Filter out failures and get the values
  const successes = results
    .filter(r => r.status === 'fulfilled')
    .map(r => r.value);

  return successes.length > 0 ? successes[0] : null;
}

Key Takeaway: Use Promise.allSettled for parallel operations where you need resilience—it guarantees you'll receive all results, allowing you to handle successes and failures independently.