Course contentsShow
← JavaScript
Lesson 1976 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

Promise.any Use Cases

Use Promise.any for fallback requests, fastest successful response, or redundant data sources.

Promise.any Use Cases

What you'll learn: How to apply Promise.any in real-world scenarios where you need the first successful result from multiple competing sources.

Now that you understand what Promise.any does, let's explore when it's the right tool for the job.

Fallback API Requests

The most common use case is calling multiple API endpoints where any successful response will do:

const result = await Promise.any([
  fetch('https://api.primary.com/data'),
  fetch('https://api.backup.com/data'),
  fetch('https://api.tertiary.com/data')
]);
// Uses whichever server responds successfully first

If your primary API is slow or down, you automatically get data from the backup without extra logic.

Fastest Successful Response

When you have redundant servers in different regions, use Promise.any to minimize latency:

const userData = await Promise.any([
  fetch('https://us-east.api.com/user/123'),
  fetch('https://eu-west.api.com/user/123'),
  fetch('https://asia.api.com/user/123')
]);
// Closest/fastest server wins

Multiple Data Sources

When data might come from different sources (cache, database, external API), and you want whichever succeeds first:

const config = await Promise.any([
  loadFromCache(),
  loadFromDatabase(),
  loadFromRemoteConfig()
]);

Image Loading with Fallbacks

Load images from CDNs with automatic fallbacks:

const imageBlob = await Promise.any([
  fetch('https://cdn1.example.com/image.jpg'),
  fetch('https://cdn2.example.com/image.jpg'),
  fetch('https://backup.example.com/image.jpg')
]);

Key Takeaway: Use Promise.any when you need resilience through redundancy—whether for failover servers, reducing latency across regions, or ensuring data availability from multiple sources. It's the "I just need one success" pattern.