JavaScript Medium technical 1 views 2 min read

What is Promise.any and when should it be used?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of JavaScript conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

Promise.any() is a Promise combinator method introduced in ES2021 that takes an iterable of promises and returns a single promise that fulfills as soon as any of the input promises fulfills. It resolves with the value of the first promise that successfully resolves.

Key Characteristics:

  1. First Success Wins: Returns the value of the first fulfilled promise
  2. Ignores Rejections: Continues waiting even if some promises reject
  3. AggregateError: Only rejects if all promises reject (with an AggregateError containing all rejection reasons)

When to Use Promise.any():

  • Fastest Resource: When fetching from multiple mirrors/CDNs and you want the first successful response
  • Redundant Services: When calling multiple redundant APIs and only need one to succeed
  • Fallback Mechanisms: When you have primary and backup data sources

Example:

     // Fetching from multiple CDNs - use whichever responds first
     const cdn1 = fetch('https://cdn1.example.com/data.json');
     const cdn2 = fetch('https://cdn2.example.com/data.json');
     const cdn3 = fetch('https://cdn3.example.com/data.json');

     Promise.any([cdn1, cdn2, cdn3])
       .then(response => response.json())
       .then(data => console.log('First successful response:', data))
       .catch(error => {
         // Only if ALL promises reject
         console.error('All CDNs failed:', error.errors);
       });

     // Comparison with other Promise methods:
     const promises = [
       Promise.reject('Error 1'),
       Promise.resolve('Success!'),
       Promise.reject('Error 2')
     ];

     // Promise.any() - Returns first fulfilled promise
     Promise.any(promises)
       .then(value => console.log(value)); // Output: "Success!"

     // Promise.race() - Returns first settled promise (fulfilled or rejected)
     Promise.race(promises)
       .catch(error => console.log(error)); // Output: "Error 1"

     // Promise.all() - Waits for all or fails on first rejection
     Promise.all(promises)
       .catch(error => console.log(error)); // Output: "Error 1"

     // Promise.allSettled() - Waits for all, never rejects
     Promise.allSettled(promises)
       .then(results => console.log(results));
     // Output: [
     //   { status: 'rejected', reason: 'Error 1' },
     //   { status: 'fulfilled', value: 'Success!' },
     //   { status: 'rejected', reason: 'Error 2' }
     // ]
     

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?