Facebook Pixel
Promise.race: Examples and Use Cases in JavaScript

What is the difference between Promise.race and Promise.any in JavaScript?

The answer

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

More FAQs in Promise.race: Examples and Use Cases in JavaScript

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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.