Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Confusion on memory leaks caused by not clearing the timeout in the fetch handlers

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!