🎯 Promise.any() Implementation
Category: js / promises
Difficulty: medium
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 [code example] 🧪 Example: [code example] ❌ If all reject: [code example] <!-- 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 -->...