Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda