DEV Community


Posted on • Originally published at

Promise.race for timeouts, fetch and avoiding memory leaks

Using Promise.race is a popular option for getting a cancellation/timeout behaviour for fetch calls, when the new AbortController is not available for use. However, there are ways to shoot yourself in the foot with this technique. I wrote a post which goes through:

1 How to use Promise.race to achieve timeouts for fetch calls
2 👾 Buggy implementation
3 ✅A safer implementation using newer features in JavaScript that won't have the problems as that in 2.

Top comments (1)

assadbintahir profile image
Asad Ullah

It would still lead to memory leaks if one of the promise never resolves. Following is the more safe solution as compared to Promise.race