I am reading blog post on Promise.race, fetch and avoiding memory leaks and there is one thing I don't quite understand
The naive implementation he mentioned in the blog post causes a memory leak
const request = fetch(url, options).then(response => response.json());
const timeout = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Timeout'));
}, 10000)
});
Promise.race([ request, timeout ]).then(response => {}, error => {})
I understand that the callback we pass to setTimeout holds references to the promise constructor’s function, but what I don't understand is that it in the Heap Snapshot he took at the end of the blog post showing that there are a lot of http.agent and related Socket objects got retained because of the inline setTimout version. Why such an implementation would cause the retaintion of http.agent and Socket objects? I thought it was going to retain the promise constructor’s function and that's it.