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

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

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 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!