AbortController
Creates a cancellation signal.
AbortController cancels async work. Streams let code process data in chunks instead of waiting for everything at once.
const controller = new AbortController();
fetch(url, { signal: controller.signal });
controller.abort();
Async JavaScript
AbortController creates a signal that can be passed to APIs such as fetch and addEventListener. Calling abort notifies those APIs that the work should stop.
Cancellation matters when users navigate away, type a new query or close a panel before async work finishes.
Streams represent data arriving over time. They are useful for large responses, progressive processing and advanced performance work.
Creates a cancellation signal.
Passed to APIs that support cancellation.
Triggers cancellation.
Process data chunk by chunk.
Examples
const controller = new AbortController();
startButton.addEventListener("click", () => runTask(controller.signal));
stopButton.addEventListener("click", () => controller.abort());
const data = await fetchResults(query); render(data); // A slower old query can overwrite a newer result.
Code patterns
These examples are the async patterns you will reuse: timers, promises, composition, await, fetch and cancellation.
Pass signal to fetch.
const controller = new AbortController();
const response = await fetch(url, { signal: controller.signal });
controller.abort();
Abort usually rejects with AbortError.
try {
await fetch(url, { signal });
} catch (error) {
if (error.name === "AbortError") return;
}
Use signal to remove listeners automatically.
element.addEventListener("click", handler, { signal });
controller.abort();
Streams expose a reader for chunks.
const reader = response.body.getReader();
const { value, done } = await reader.read();
Rules that matter
Async JavaScript is easier when every operation has clear pending, success, failure and cancellation behavior.
Old requests should not overwrite newer UI.
APIs need the signal before work starts.
Cancellation is often expected, not a failure message.
AbortController can clean temporary listeners.
Do not load huge bodies into memory unnecessarily.
The UI should know whether work finished, failed or was cancelled.
Production thinking
Without cancellation, async interfaces can show stale results and waste resources.
Cancelled loading should leave users with a clear state, not an endless busy message.
Production search, upload and navigation flows often need cancellation to avoid race conditions.
Cancellation is mostly runtime behavior, but stale async rendering can produce confusing page states.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain when the code runs, how it fails and what happens if it becomes irrelevant, you understand the async model.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Create a mock fetch flow with loading, success, error and abort behavior.
What happens if the request fails twice in a row?