Explorer
Node.js

Write a function `promiseAny(promises)` that resolves with the first fulfilled promise. Rejects only if ALL reject.

Problem Statement

Write a function `promiseAny(promises)` that resolves with the first fulfilled promise. Rejects only if ALL reject.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Promise.any() resolves as soon as one promise fulfills. šŸ’” Hint 2: Track rejections; if all reject, reject with an AggregateError. šŸ’” Hint 3: Count rejections and reject when rejectCount === promises.length. āœ… Solution: ```javascript function promiseAny(promises) { return new Promise((resolve, reject) => { let rejections = 0; const errors = []; promises.forEach((p, i) => { Promise.resolve(p).then(resolve).catch(err => { errors[i] = err; rejections++; if (rejections === promises.length) reject(errors); }); }); }); } ```

Implement Promise.any Polyfill

Medium
Write a function `promiseAny(promises)` that resolves with the first fulfilled promise. Rejects only if ALL reject.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
"ok"