Promise.any (First Fulfilled)
Arguments
iterable: Iterable<T | PromiseLike<T>> —A synchronous iterable of values or promise-like inputs, consumed in iteration order.
Returns
Promise<T> — Solution idea
Consume the synchronous iterable with for...of, assign each entry an iteration index, and normalize it with Promise.resolve. Use a sentinel remaining count so empty input rejects without relying on array length and sparse arrays behave like native array iteration. Store rejection reasons by iteration index so completion timing cannot reorder AggregateError.errors.
promiseAny([
Promise.reject(new Error('A')),
Promise.resolve('ok'),
Promise.resolve('later')
]).then(v => {
// v === 'ok'
});
promiseAny([Promise.reject('x'), Promise.reject('y')])
.catch(err => {
// err.name === 'AggregateError'
// err.errors === ['x', 'y']
});Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- LeetCode Discuss: Intuit senior frontend engineer interview experience, November 2025
One senior account describes explaining Promise.race versus Promise.any and then coding a polyfill for Promise.race. Promise.any is the sibling drill; it is not the reported exercise.
Reported in a public Intuit interview account. Intuit frontend interview questions
Solution
Overview
Consume the synchronous iterable with for...of, assign each entry an iteration index, and normalize it with Promise.resolve. Use a sentinel remaining count so empty input rejects without relying on array length and sparse arrays behave like native array iteration. Store rejection reasons by iteration index so completion timing cannot reorder AggregateError.errors.
Approach: Count rejections and aggregate reasons
Algorithm:
1) Return a base Promise and initialize errors, nextIndex, and remaining = 1 as an iteration sentinel.
2) Iterate with for...of so arrays, sparse arrays, Sets, and generators share one contract.
3) Reserve an index and increment remaining before attaching each Promise.resolve handler.
4) Resolve immediately on any fulfillment.
5) Store each rejection at its reserved index; decrement remaining and reject with native AggregateError when it reaches zero.
6) After iteration, remove the sentinel; zero now means the iterable was empty.
This mirrors Promise.any for synchronous iterable inputs and native Promise results, but it is not a generic Promise-constructor method and does not preserve Promise subclasses. Complexity: O(n) time and O(n) space for stored errors.
export default function promiseAny(iterable) {
return new Promise((resolve, reject) => {
const errors = [];
let nextIndex = 0;
let remaining = 1;
try {
for (const item of iterable) {
const index = nextIndex++;
remaining += 1;
Promise.resolve(item).then(
resolve,
(reason) => {
errors[index] = reason;
remaining -= 1;
if (remaining === 0) {
reject(new AggregateError(errors, 'All promises were rejected'));
}
}
);
}
} catch (error) {
reject(error);
return;
}
remaining -= 1;
if (remaining === 0) {
reject(new AggregateError(errors, 'All promises were rejected'));
}
});
}
export default function promiseAny<T>(
iterable: Iterable<T | PromiseLike<T>>
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const errors: unknown[] = [];
let nextIndex = 0;
let remaining = 1;
try {
for (const item of iterable) {
const index = nextIndex++;
remaining += 1;
Promise.resolve(item).then(
resolve,
(reason) => {
errors[index] = reason;
remaining -= 1;
if (remaining === 0) {
reject(new AggregateError(errors, 'All promises were rejected'));
}
}
);
}
} catch (error) {
reject(error);
return;
}
remaining -= 1;
if (remaining === 0) {
reject(new AggregateError(errors, 'All promises were rejected'));
}
});
}
Notes & Pitfalls
Using Promise.race is incorrect because it rejects on the first rejection. Using array length plus forEach leaves sparse-array holes unobserved and can keep the result pending forever. Storing reasons in completion order violates AggregateError's required input-order contract.
new Array(1) iterates one undefined value and therefore fulfills with undefined. Empty input and all-rejected input produce native AggregateError values. Sets, generators, and other synchronous iterables are accepted; async-only iterables are outside this contract.
Promise.resolve to normalize inputs. Sentinel rejection counting independent of array length. Native AggregateError with reasons stored by iteration index.
Common mistakes on this challenge
promiseAny fulfillment/rejection contract is incomplete
Promise.any consumes an iterable, treats sparse holes as undefined, and preserves reason order. Consume the synchronous iterable with for...of; do not combine array length withforEach.Use a sentinel remaining count so empty input and sparse arrays settle correctly. Store rejection reasons by iteration index and reject with native AggregateErroronly when all reject.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.