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

117
Vistas
How to reject child Promise using AbortController

I just learned about how to reject a Promise by using the AbortController API. It's working fine but when the promise to reject as a "child Promise", this one will still continue running even if the parent Promise is rejected.

Is there a way, when the parent is rejected, to stop the children too?

In this example, this means that there is no more running log after the rejected log when you click on the cancel button:

let controller = new AbortController();

new Promise((resolve, reject) => {
  setInterval(() => console.log("running"), 500)

  controller.signal.addEventListener('abort', () => {
    console.log("rejected")
    reject();
  });

});

function cancel() {
  controller.abort();
}
<button onclick="cancel()">Cancel async loop</button>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Promise and abort controller are fine it's your timer function that needs to be cleared.

When you declare setInterval it returns an id that can be referenced later to clear the timer.

So, when you reject the promise you need to clear it.

let controller = new AbortController();

new Promise((resolve, reject) => {
  const id = setInterval(() => console.log("running"), 500)

  controller.signal.addEventListener('abort', () => {
    console.log("rejected");
    clearInterval(id);
    reject();
  });

});

function cancel() {
  controller.abort();
}
<button onclick="cancel()">Cancel async loop</button>

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