Problem, as an interviewer would state it:
"Implement Promise.any(promises) — resolving with the value of the FIRST promise to fulfill, and REJECTING with a real AggregateError (containing every individual rejection reason) only if EVERY promise rejects, matching real native Promise.any's own documented contract."
Examples:
Promise.any([Promise.reject("x"), Promise.resolve("y")]); // resolves "y"
Promise.any([Promise.reject("a"), Promise.reject("b")]); // rejects AggregateError
Clarifying questions expected:
- How does this genuinely differ from
Promise.race— does an EARLIER rejection need to be correctly ignored if a LATER promise still fulfills? - What real, specific error type should be thrown when every promise rejects — a plain
Error, or the real, dedicatedAggregateError? - What is the real, correct behavior for a genuinely empty input array?
Code / implementation expected: Yes — real, direct proof it resolves on the first fulfillment despite an earlier rejection, and throws a real AggregateError (populated with every individual rejection reason) only when every promise genuinely rejects.