Course contentsShow
← JavaScript
Lesson 2304 of 2,72347. Networking PrimitivesFree lesson

Reusing and One-Time Signals

Understand that an AbortController can only abort once and must be recreated for subsequent operations.

Reusing and One-Time Signals

You'll learn why you need a fresh AbortController for each new cancellable operation.

The One-Shot Nature of AbortController

Once an AbortController calls its abort() method, the associated signal becomes permanently aborted—there's no "reset" button. This is a crucial design decision: signals fire once and stay fired.

Think of it like a fire alarm. Once you pull it, that alarm has been triggered. You can't un-pull it and use the same alarm lever again. You'd need to reset the system (create a new controller) for the next emergency.

Why This Matters

If you're canceling multiple sequential requests, you might be tempted to reuse the same controller:

const controller = new AbortController();

// First request
fetch('/api/data1', { signal: controller.signal })
  .catch(err => console.log('Aborted:', err.name));

controller.abort(); // Cancels first request

// This will IMMEDIATELY fail because signal is already aborted!
fetch('/api/data2', { signal: controller.signal })
  .catch(err => console.log('Also aborted:', err.name));

Both requests fail because the signal is already in the aborted state from the first abort() call.

The Solution: Fresh Controllers

For each new cancellable operation, create a new AbortController:

let controller = new AbortController();

fetch('/api/data1', { signal: controller.signal });
controller.abort();

// Create a fresh controller for the next operation
controller = new AbortController();
fetch('/api/data2', { signal: controller.signal });
// This one works fine—new signal, clean slate

Key Takeaway: An AbortController can only abort once; always create a new controller for each independent cancellable operation.