Facebook Pixel

Promise.any: Examples and Use Cases in JavaScript

Promise.any returns the first to fulfill. Here are practical examples and AggregateError.

Promise.any: Examples and Use Cases in JavaScript

Promise.any returns as soon as the first promise fulfills. It ignores rejections until all reject. Here are practical examples.

First Successful Source

const fastest = await Promise.any([ fetchFromCDN(), fetchFromOrigin(), fetchFromMirror(), ]); `` Use whichever source succeeds first. If all fail, it rejects with `AggregateError`. ### Best Effort with Fallback ```js try { const result = await Promise.any([ primaryAPI(), secondaryAPI(), tertiaryAPI(), ]); render(result); } catch (err) { console.error("All APIs failed:", err.errors); // AggregateError render(fallbackData); } `` ### AggregateError If all promises reject, `Promise.any` rejects with an `AggregateError`: ```js try { await Promise.any([ Promise.reject(new Error("A failed")), Promise.reject(new Error("B failed")), ]); } catch (err) { console.error(err instanceof AggregateError); // true console.error(err.errors); // [Error: A failed, Error: B failed] } `` `err.errors` is an array of all the individual rejection reasons. ### Difference from Promise.race ```js // race: first to settle (can be a rejection) const r = await Promise.race([Promise.reject("err"), Promise.resolve("ok")]); // could throw "err" or return "ok", depending on which settles first // any: first to fulfill (ignores rejections) const a = await Promise.any([Promise.reject("err"), Promise.resolve("ok")]); // returns "ok" (ignores the rejection) `` ### Empty Array ```js const result = await Promise.any([]); // rejects with AggregateError (all reject) `` `Promise.any([])` rejects immediately with an `AggregateError` (no promises to fulfill). ### The Takeaway `Promise.any` returns the first promise to fulfill. It ignores rejections until all reject. Use for first successful source and best-effort with fallback. If all reject, it throws `AggregateError` with `errors` array. Different from `race` (which returns the first to settle, even a rejection).

It returns as soon as the first promise fulfills. It ignores rejections until all promises reject. If all reject, it rejects with an AggregateError containing all the rejection reasons.

Promise.race returns the first to settle (fulfilled or rejected). Promise.any returns the first to fulfill (ignores rejections). race can reject on the first rejection; any only rejects if all reject (with AggregateError).

An error type thrown by Promise.any when all promises reject. It has an errors property (an array of all individual rejection reasons). Check err instanceof AggregateError and access err.errors.

It rejects with an AggregateError (all promises reject, and there are no promises to fulfill). This is different from Promise.race([]), which never settles.

When you want the first successful result from multiple sources. For example, fetching from multiple API endpoints or mirrors and using whichever responds first. If all fail, you get an AggregateError with all reasons.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.