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

328
Visualizações
If an empty Promise is encountered within an infinite while loop, why is the while loop resolved with a pending Promise?

If I make a Promise that is never fulfilled:

const nothingPromise = new Promise((resolve) => {});

And then I await that Promise within an infinite while loop:

async function run() { while (true) { await nothingPromise;}}

Any then() function attached to the function will not run, but I don't get an infinite loop either. I get a pending Promise. In the Chrome console:

run().then(() => console.log('then')) Promise {<pending>}

Why is a pending Promise returned? I have a feeling that it has something to do with this part of the ECMAScript spec:

The abstract operation LoopContinues takes arguments completion and labelSet and returns a Boolean. It performs the following steps when called:

  1. If completion.[[Type]] is normal, return true.
  2. If completion.[[Type]] is not continue, return false.
  3. If completion.[[Target]] is empty, return true.
  4. If completion.[[Target]] is an element of labelSet, return true.
  5. Return false.

But I don't know which completion condition corresponds to await nothingPromise.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

await sends the function it is in to sleep until:

  • The promise resolves and
  • The main event loop is free

So the while loop starts, the promise is awaited, and the function which calls run() receives the promise returned by run (which is pending because run is asleep) and continues.


Since nothingPromise never resolves, the run function never wakes up, so never completes, and never resolves the promise it returns.


The part of the specification you found is irrelevant since await sends run to sleep in the middle of the first iteration of the loop, so the loop never reaches completion.

about 4 years ago · Juan Pablo Isaza Relatório
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