Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda