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:
- If completion.[[Type]] is normal, return true.
- If completion.[[Type]] is not continue, return false.
- If completion.[[Target]] is empty, return true.
- If completion.[[Target]] is an element of labelSet, return true.
- Return false.
But I don’t know which completion condition corresponds to await nothingPromise
.
Advertisement
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 await
ed, 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.