Facebook Pixel

Promise.race: Examples and Use Cases in JavaScript

Promise.race returns the first to settle. Here are practical examples including timeouts.

Promise.race: Examples and Use Cases in JavaScript

Promise.race returns as soon as the first promise settles (fulfilled or rejected). Here are practical examples.

Timeout

function withTimeout(promise, ms) { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), ms) ); return Promise.race([promise, timeout]); } const result = await withTimeout(fetch("/api"), 5000); `` If `fetch` takes more than 5 seconds, the timeout rejects first, and the race rejects. ### First Successful Source ```js const fastest = await Promise.race([ fetchFromCDN(), fetchFromOrigin(), ]); `` Use whichever source responds first. ### Cancellation (Simplified) ```js function cancellableFetch(url, signal) { return Promise.race([ fetch(url), new Promise((_, reject) => signal.addEventListener("abort", () => reject(new Error("cancelled")))), ]); } `` If the signal aborts before the fetch completes, the race rejects. ### Empty Array ```js const result = await Promise.race([]); // never settles (stays pending forever) `` `Promise.race([])` never settles. Be careful not to pass an empty array. ### The First to Settle Can Be a Rejection ```js const result = await Promise.race([ Promise.resolve("success"), Promise.reject(new Error("fail")), ]); // could be "success" or could throw, depending on which settles first `` If the rejection settles first, the race rejects. This is different from `Promise.any`, which ignores rejections. ### The Takeaway `Promise.race` returns the first promise to settle (fulfilled or rejected). Use cases: timeouts, first successful source, cancellation. If the first to settle is a rejection, the race rejects. `Promise.race([])` never settles.

It returns as soon as the first promise settles (fulfilled or rejected). If the first to settle fulfills, the race fulfills with its value. If the first to settle rejects, the race rejects with its reason.

Race the operation against a timeout promise: Promise.race([fetch('/api'), new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 5000))]). If the timeout fires first, the race rejects.

Yes. If the first promise to settle rejects, the race rejects with that reason. This is different from Promise.any, which ignores rejections and waits for the first fulfillment.

It never settles (stays pending forever). Be careful not to pass an empty array to Promise.race. If you dynamically build the array, check that it is not empty.

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

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.