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.

Published: Jun 10, 2026Updated: Jun 10, 2026Reading time: 5 minViews: 0
AbortControllerfetchJavaScriptcancellationbeginners

💡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

Programming code illustration
Programming code illustration

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

You open a food delivery app. The app is loading restaurants in one area. You switch to another area. The old loading task is no longer needed.

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

Stop the old task. I do not need that result anymore.

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.

JAVASCRIPT
const controller = new AbortController();

This creates a cancel controller.

JAVASCRIPT
controller.abort();

This presses cancel.

Memory tip:

TEXT
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

shirt

The website starts loading results for “shirt”. Then the user continues typing:

Example

winter shirt

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

JAVASCRIPT
const controller = new AbortController();

fetch("https://example.com/data", {
    signal: controller.signal
});

controller.abort();

Line-by-line:

JAVASCRIPT
const controller = new AbortController();

Create a cancel button.

Example

signal: controller.signal

Give the cancel signal to fetch.

JAVASCRIPT
controller.abort();

Press cancel.

Real-life meaning:

Example

I start a task. I attach a cancel button to it. When I do not need it anymore, I press cancel.

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.

JAVASCRIPT
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

JAVASCRIPT
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:

TEXT
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

JAVASCRIPT
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

The user searches for "shirt". The program starts loading. The user types "winter shirt". The program cancels the "shirt" request. The program loads results for "winter shirt".

8. Important: do not reuse an already-canceled controller

A common mistake is reusing an old controller after canceling it.

Bad idea:

JAVASCRIPT
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

A cancel button that was already pressed should not be reused for a new task.

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

AbortController is like someone saying “stop”. But the task must know how to listen.

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

Product search boxes. Product detail pages. Maps loading locations. Chat apps loading old messages. Forms sending data while the user clicks cancel.

11. Common mistakes

Mistake 1: Forgetting to pass signal to fetch

Wrong:

JAVASCRIPT
const controller = new AbortController();

fetch("https://example.com/data");

controller.abort();

Correct:

JAVASCRIPT
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.

JAVASCRIPT
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

JAVASCRIPT
const controller = new AbortController();

fetch("https://example.com/data", {
    signal: controller.signal
});

Answer 3

JAVASCRIPT
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:

TEXT
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

  1. MDN Web Docs — AbortController: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
  2. MDN Web Docs — AbortSignal: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
  3. MDN Web Docs — AbortController.abort(): https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
  4. MDN Web Docs — Fetch API: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
  5. Wikimedia Commons — programming image: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
PR

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`.