Facebook Pixel

What does Promise.race do 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.

Verify This Answer

Cross-check this information using these trusted sources:

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

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.

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.