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

148
Views
Can promises returned from async functions be garbage collected?

As it was discussed earlier, JavaScript promises have nothing special with respect to garbage collecting. In the other hand, both async functions and await operations are wrappers around promises. So I wonder whether it is possible for the promise returned from an async function be GCed if no one holds a reference to it and internal awaiting takes too much time?

function getDelayedPromise() {
  return new Promise((resolve) => setTimeout(resolve, someBigNumber));
}

async function bar(data) {
  await getDelayedPromise(data);
  return data;
}

function DOMEventHandler() {
  bar().then(updateUI);
}

(Let's suppose DOMEventHandler sunchronously gets runned to handle some DOM event)

Under the hood, await fulfills then() on the delayed promise to continue function execution after the promise be settled. However, the promise associated with an async function itself (one returned from the bar()) is not placed in the memory in this way. In fact, the spec prescribes to it wait for async function completion to be settled with its value thereafter:

a. Let result be the result of evaluating asyncBody. 
...
d. If result.[[Type]] is normal, then    
   i. Perform ! Call(promiseCapability.[[Resolve]], undefined, « undefined »).
...

(where promiseCapability represents an async function promise)

Is this waiting a guarantee of bar() promise should not be collected before an async function body is evaluated? In other words, is there likelihood that updateUI() will never be executed?

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!