Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda