When to Use Promise.race()

Learn how Promise.race() can be used as a timeout for an operation.

Establishing a timeout for an operation

While the fetch() function has a lot of helpful functionalities, one thing it doesn’t do is manage a timeout for a given request; a request will happily hang until it completes one way or another. We can easily create a wrapper method to add a timeout to any request by using Promise.race():

Get hands-on with 1200+ tech skills courses.