Promise.any (First Fulfilled)

intermediate
By FrontendAtlas Editorial · Updated Nov 12, 2026
Implement promiseAny(iterable) for synchronous iterables of values and promises. Resolve with the first fulfillment, ignore rejections until every iterated input rejects, and reject empty or all-rejected input with a native AggregateError whose errors preserve iteration order. Sparse array holes are iterated as undefined and therefore fulfill.

Arguments

  • iterable: Iterable<T | PromiseLike<T>> — A synchronous iterable of values or promise-like inputs, consumed in iteration order.

Returns

Promise<T> — Resolves to the first fulfilled value, or rejects with AggregateError when all reject.

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.

Examples
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.

Reported in a public Intuit interview account. Intuit frontend interview questions