CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

🎯 Promise.any() Implementation

Promise.any resolves as soon as any promise succeeds. Rejects only if all promises fail, returning an AggregateError.

takes an iterable of promises and returns a single promise that resolves as soon as any of the input promises fulfills. If all promises reject, it rejects with an containing all rejection reasons. -- 🧠 Understanding Promise.any -- Atom 1: Purpose returns a promise that: βœ… Resolves as soon as any input promise resolves ❌ Rejects only if all input promises reject -- Atom 2: Normalization Convert all inputs to promises with -- Atom 3: Resolve on First Fulfilled On first , resolve the outer promise. -- Atom 4: Track Rejections Maintain count of rejections. If all promises reject, reject with , which contains all rejection reasons. -- Atom 5: Edge Case If input is empty β†’ reject immediately with -- βœ… Implementation: -- πŸ§ͺ Example: ❌ If all reject: -- <!-quiz-start --Q1: When does Promise.any reject? [ ] When the first promise rejects [ ] When any promise rejects [x] Only when ALL promises reject [ ] It never rejects Q2: What type of error does Promise.any throw when all promises reject? [ ] TypeError [ ] RejectionError [x] AggregateError [ ] PromiseError Q3: What happens when Promise.any is passed an empty array? [ ] It resolves with undefined [ ] It returns a pending promise that never settles [x] It rejects immediately with an AggregateError [ ] It throws a TypeError <!-quiz-end --
JavaScriptPromises
🎯 Promise.all() Implementation
hard
βœ… Promise.allSettled() Implementation
medium
🎯 Promise.any() Implementation
medium
πŸ›‘ Cancelable Promise Implementation
medium
πŸ”§ Custom Promise Class Implementation
hard
🏁 Promise.race() Implementation
medium
πŸ”„ Promise Retry Implementation
easy
πŸ“‹ Sequential Promise Execution
medium
3 of 8
LibraryJavaScriptPromises42 of 61

🎯 Promise.any() Implementation

jspromisesmedium

Promise.any() takes an iterable of promises and returns a single promise that resolves as soon as any of the input promises fulfills. If all promises reject, it rejects with an AggregateError containing all rejection reasons.


🧠 Understanding Promise.any


Atom 1: Purpose

Promise.any returns a promise that:

  • βœ… Resolves as soon as any input promise resolves
  • ❌ Rejects only if all input promises reject

Atom 2: Normalization

  • Convert all inputs to promises with Promise.resolve(...)

Atom 3: Resolve on First Fulfilled

  • On first .then, resolve the outer promise.

Atom 4: Track Rejections

  • Maintain count of rejections.
  • If all promises reject, reject with AggregateError , which contains all rejection reasons.

Atom 5: Edge Case

  • If input is empty β†’ reject immediately with AggregateError

βœ… Implementation: promiseAny

function promiseAny(iterable) {
  return new Promise((resolve, reject) => {
    const promises = Array.from(iterable);
    const errors = [];
    let rejectedCount = 0;

    if (promises.length === 0) {
      return reject(new AggregateError([], "All promises were rejected"));
    }

    promises.forEach((p, i) => {
      Promise.resolve(p)
        .then(resolve)
        .catch((err) => {
          errors[i] = err;
          rejectedCount++;
          if (rejectedCount === promises.length) {
            reject(new AggregateError(errors, "All promises were rejected"));
          }
        });
    });
  });
}

πŸ§ͺ Example:

promiseAny([
  Promise.reject("fail1"),
  new Promise(res => setTimeout(() => res("success"), 100)),
  Promise.reject("fail2")
]).then(console.log)
  .catch(console.error); // β†’ "success"

❌ If all reject:

promiseAny([
  Promise.reject("fail1"),
  Promise.reject("fail2")
]).then(console.log)
  .catch(err => {
    console.error(err instanceof AggregateError); // true
    console.error(err.errors);                   // ["fail1", "fail2"]
  });

Quick Quiz

Test your understanding with 3 quick questions

Q1When does Promise.any reject?
Q2What type of error does Promise.any throw when all promises reject?
Q3What happens when Promise.any is passed an empty array?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna