Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda