Creating an AbortController and Signal
You'll learn how to create an AbortController and access its signal to enable request cancellation.
Now that you understand why AbortController exists, it's time to actually create one and use its signal. The pattern is straightforward: instantiate an AbortController, grab its signal property, and pass that signal to APIs like fetch().
The Basic Pattern
Creating an AbortController is simple:
const controller = new AbortController();
This gives you a controller object with two key members:
signal— A property containing the AbortSignal you'll pass to fetch or other APIsabort()— A method you'll call later to cancel the operation (covered in future lessons)
Accessing the Signal
The signal is what you pass to APIs that support cancellation:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.example.com/data', { signal })
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error('Fetch failed:', err));
Think of the AbortController as a remote control and the signal as an infrared beam. The remote (controller) stays in your hand, while the beam (signal) connects to the TV (fetch request). When you press the button later, the signal communicates the cancellation.
Why Separate Controller and Signal?
This separation is intentional: the signal is read-only and passed to potentially untrusted code, while the controller with its abort() method stays in your control. The receiving API can only listen to the signal, not trigger cancellation itself.
Key Takeaway: Create an AbortController with new AbortController(), access its signal property, and pass that signal to fetch or other abort-aware APIs to enable cancellation.