DEV Community

Kashyap
Kashyap

Posted on • Originally published at kgrz.io

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.

https://kgrz.io/avoiding-memory-leaks-timing-out-fetch.html

Top comments (1)

Collapse
 
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

github.com/nodejs/node/issues/1746...