Skip to content
Advertisement

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.

Answer

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.

Advertisement