JavaScript Guides
Practical JavaScript: Canceling Work with AbortController
A beginner-friendly guide that explains how to use AbortController in JavaScript to cancel unnecessary fetch requests, with simple examples, common mistakes, and practice exercises.
💡Key Takeaways
- A beginner-friendly guide that explains how to use AbortController in JavaScript to cancel unnecessary fetch requests, with simple examples, common mistakes, and practice exercises.
Topic: practical JavaScript for beginners
Audience: people who know a little about console.log, variables, and if / else
Level: beginner-friendly, with minimal technical terms
Main idea: when the user changes their mind, your program should be able to stop work that is no longer needed
Image format: internet-hosted JPEG image, no base64 embedding, no SVG

Image source: Wikimedia Commons — Colorful code (Unsplash).jpg.
Photo author: Markus Spiske.
License: CC0 1.0 Public Domain Dedication according to the Wikimedia Commons page.
Original image page: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
Direct display image URL: https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Colorful_code_%28Unsplash%29.jpg/1280px-Colorful_code_%28Unsplash%29.jpg
1. Why should you learn this topic?
Many JavaScript beginners start with variables, conditions, loops, and functions. Those are important. But in real websites, you will meet another problem: how to cancel work that is still running when it is no longer needed.
Real-life example:
Example
If the app keeps loading old data, it may waste network usage, slow down the page, or show the wrong result. So the program needs a way to say:
Example
In JavaScript, one way to do this is AbortController.
2. Simple idea: AbortController is a cancel button
Do not treat AbortController as a scary word. Think of it as a cancel button.
const controller = new AbortController();
This creates a cancel controller.
controller.abort();
This presses cancel.
Memory tip:
AbortController = the cancel button
abort() = press cancel
signal = the wire that tells the running task it was canceled
3. When do we need cancellation?
A common example is a search box.
The user types:
Example
The website starts loading results for “shirt”. Then the user continues typing:
Example
Now the result for “shirt” may no longer be needed. We only need the result for “winter shirt”. If the old request finishes after the new one, the page may show the wrong result.
This is a problem beginners often do not think about, but it appears often in real apps.
4. The smallest example
const controller = new AbortController();
fetch("https://example.com/data", {
signal: controller.signal
});
controller.abort();
Line-by-line:
const controller = new AbortController();
Create a cancel button.
Example
Give the cancel signal to fetch.
controller.abort();
Press cancel.
Real-life meaning:
Example
5. Example with error handling
When a loading task is canceled, the code may go into catch. Beginners can think of catch as the place where we handle something that did not finish normally.
const controller = new AbortController();
async function loadData() {
try {
const response = await fetch("https://example.com/data", {
signal: controller.signal
});
const data = await response.json();
console.log(data);
} catch (error) {
console.log("Canceled or an error happened");
}
}
loadData();
controller.abort();
You do not need to fully understand async and await yet. For now, think of them as a cleaner way to write code that waits for data.
6. Practical example: Load and Cancel buttons
let controller;
async function startLoading() {
controller = new AbortController();
try {
console.log("Start loading...");
const response = await fetch("https://example.com/data", {
signal: controller.signal
});
const data = await response.json();
console.log("Loaded:", data);
} catch (error) {
console.log("Loading was canceled or failed.");
}
}
function cancelLoading() {
controller.abort();
console.log("Cancel clicked.");
}
Meaning:
startLoading() starts loading data.
cancelLoading() cancels the current loading task.
On a real web page, startLoading() can be connected to a “Load data” button, and cancelLoading() can be connected to a “Cancel” button.
7. Product search example
let controller;
async function searchProducts(keyword) {
if (controller) {
controller.abort();
}
controller = new AbortController();
try {
const response = await fetch("/search?q=" + keyword, {
signal: controller.signal
});
const data = await response.json();
console.log("New results:", data);
} catch (error) {
console.log("Old request was canceled.");
}
}
How it works:
Example
8. Important: do not reuse an already-canceled controller
A common mistake is reusing an old controller after canceling it.
Bad idea:
const controller = new AbortController();
controller.abort();
fetch("https://example.com/data", {
signal: controller.signal
});
The controller has already been canceled. If you reuse it, the new task may be canceled immediately.
Memory tip:
Example
Create a new controller for each new task.
9. AbortController does not magically stop everything
AbortController sends a cancel signal. The running task must know how to listen to that signal.
fetch can receive signal, so it can be canceled. But not every piece of code automatically stops just because you created an AbortController.
Simple meaning:
Example
10. When should you use it?
Think about AbortController when:
- the user can click cancel;
- the user leaves the page;
- the user types search text quickly;
- old data is no longer needed;
- you want to avoid showing old results;
- you want to reduce unnecessary loading.
Real examples:
Example
11. Common mistakes
Mistake 1: Forgetting to pass signal to fetch
Wrong:
const controller = new AbortController();
fetch("https://example.com/data");
controller.abort();
Correct:
const controller = new AbortController();
fetch("https://example.com/data", {
signal: controller.signal
});
controller.abort();
Mistake 2: Reusing a canceled controller
After calling abort(), create a new controller for the next task.
Mistake 3: Thinking cancellation is always a serious error
When you cancel, the code may go to catch. This is not always a serious error. Sometimes it simply means the user canceled or changed their mind.
12. Small exercises
Exercise 1
Create an AbortController and print controller.signal.
const controller = new AbortController();
console.log(controller.signal);
Exercise 2
Write a fetch call that receives a signal.
Exercise 3
Write a cancelLoading() function that cancels loading.
Exercise 4
Explain in your own words: why should a search box cancel old requests when the user keeps typing?
13. Suggested answers
Answer 2
const controller = new AbortController();
fetch("https://example.com/data", {
signal: controller.signal
});
Answer 3
let controller = new AbortController();
function cancelLoading() {
controller.abort();
}
Answer 4
Because the old result may no longer match what the user is searching for. If the old request is not canceled, the website may waste network usage, become slower, or show old results after the user has already typed a new keyword.
14. Quick memory notes
AbortController is a cancel button.
controller.signal is the signal sent to the running task.
controller.abort() presses cancel.
Use it when old work is no longer needed.
It is useful with fetch, search boxes, page changes, cancel buttons, and user interfaces.
A new task should usually have a new controller.
15. Conclusion
AbortController is very useful in real websites. It helps handle situations where the user changes their mind, leaves the page, clicks cancel, or types too quickly.
The simplest way to remember it:
AbortController = create a cancel button.
signal = give the cancel button to the task.
abort() = press cancel.
If you already know variables, functions, and fetch, this is a useful next topic.
SEO title suggestions
- What Is AbortController in JavaScript? Beginner-Friendly Explanation
- How to Cancel a Fetch Request in JavaScript with AbortController
- Practical JavaScript: Cancel Work When the User Changes Their Mind
- Practical JavaScript: AbortController and signal
SEO meta description
A beginner-friendly explanation of AbortController in JavaScript, including real-life examples, canceling fetch requests, signal, abort, common mistakes, and practice exercises.
References
- MDN Web Docs — AbortController: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
- MDN Web Docs — AbortSignal: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
- MDN Web Docs — AbortController.abort(): https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
- MDN Web Docs — Fetch API: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
- Wikimedia Commons — programming image: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
Written by PixelRouter Editorial Team
We publish deep, authoritative guides on AI infrastructure, API gateway security, cloud financial management, and system optimizations for developers.
FAQ
What is AbortController in JavaScript?
AbortController is a built‑in API that acts like a cancel button. You create an instance with `new AbortController()`, pass its `signal` to a task such as `fetch`, and call `controller.abort()` to signal that the task should stop.
How do I cancel a fetch request with AbortController?
Create a controller, pass `controller.signal` in the fetch options, and later call `controller.abort()`. Example: ```js const controller = new AbortController(); fetch('https://example.com/data', { signal: controller.signal }); controller.abort(); ```
When should I use AbortController?
Use it whenever a request may become unnecessary, such as when the user clicks a cancel button, leaves the page, types quickly in a search box, or you want to avoid showing old results and waste network usage.
What are common mistakes when using AbortController?
Common mistakes include forgetting to pass the `signal` to `fetch`, reusing a controller after it has been aborted, and treating a cancellation error as a serious failure. A new task should always get a fresh `AbortController`.