Promise.race(): First Promise to Settle Wins

Promise.race() returns a promise that mirrors the outcome of the first promise in a set to finish—the winner takes all, whether it resolves or rejects. Use it to set a timeout on a network request.
Promise.race() takes an array of promises and returns a new promise that settles as soon as the first input promise settles. The winner takes all: if the fastest promise fulfills, race() fulfills; if it rejects, race() rejects. This is perfect for time-sensitive operations, like racing a network request against a setTimeout promise to enforce a timeout. The footgun: a single, fast-rejection will cause the entire race() to reject, even if other, slower promises would have fulfilled.
Read the original → developer.mozilla.org
- #javascript
- #asynchronous
- #promises
- #node.js
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.