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

91
Views
Promise cause to stackoverflow with infinity while loop

I am trying to understand why I am getting stackoverflow error following this code:

const promise = new Promise((res,rej) => {
    res(4);
})

while(true){
    promise.then((v) => v)
}

I am expecting to get

4
4
4
4
.
.
.

but get:

FATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try using a loop inside of an async function like this as suggested below

async function someFunction(){
    while(true) {
        console.log(await promise);
    }
}
someFunction();
about 4 years ago · Juan Pablo Isaza Report

0

I'm going to illustrate how you might create an infinite series of promises that doesn't have this problem, but warning: your tab will get stuck:

const pr = new Promise((res,rej) => {
    res(4);
});

(async () => {
  let promiseList = [];
  while(true){
    const p = pr.then((v) => console.log(v));
    promiseList.push(p);
    if (promiseList.length >= 20) {
       await Promise.all(promiseList);
       promiseList = [];
    }
  }
})();

Check your console and you'll see the 4s.

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!