Facebook Pixel

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

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

Verify This Answer

Cross-check this information using these trusted sources:

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?

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
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.