Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!