Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript AbortController

The AbortController Object lets you cancel asynchronous operations.

It is commonly used to cancel fetch() requests.

Canceling unnecessary operations can improve performance and make applications more responsive.

Why Cancel a Request?

Sometimes an asynchronous operation is no longer needed.

Examples include:

  • The user clicks a Cancel button.
  • The user starts another search.
  • The user leaves the page.
  • A request takes too long.

In these situations, you can cancel the operation instead of waiting for it to finish.


Creating an AbortController

Create an AbortController with the new keyword.

Example

const controller = new AbortController();

The Abort Signal

An AbortController provides an AbortSignal.

The signal is passed to an asynchronous operation.

The operation receives the signal and stops.

Example

const controller = new AbortController();
const signal = controller.signal;

Canceling a Fetch Request

Pass the controller's signal to fetch().

Example

const controller = new AbortController();

fetch("largefile.txt", {
  signal: controller.signal
});

Aborting a Request

Call abort() to cancel the operation.

Example

controller.abort();


Complete Example

Example

const controller = new AbortController();

// Async function to download a file
async function loadFile(file) {
  try {
    const response = await fetch(file, {
      signal: controller.signal
    });

    myDisplayer(await response.text());

  } catch(err) {
    if (err.name == "AbortError") {
      myDisplayer("Download canceled");
    }
  }
}

// Call the async function
loadFile("fetch.txt");
Try it Yourself »

Cancel with a Button

A request can be canceled when the user clicks a button.

Example

<button onclick='loadFile("fetch.txt")'>Start Download</button>

<button onclick="controller.abort()">Cancel Download</button>

<p id="demo"></p>
Try it Yourself »

Create a New Controller for Each Request

Once an AbortController has been aborted, it cannot be reused.

Create a new controller for each new request.


Cancel the Previous Request

Search applications often cancel an earlier request before starting a new one.

Example

let controller;

async function search(text) {
  if (controller) {
    controller.abort();
  }

  controller = new AbortController();

  try {
    const response = await fetch(
    "/search?q=" + text,
    {signal: controller.signal}
  );
    console.log(await response.text());
  }
  catch(err) {
    if (err.name != "AbortError") {
      console.log(err);
    }
  }
}
User types:

c
ca
cat
cats

↓

Only the last request finishes.

Cancel After a Timeout

You can combine timers with AbortController)=.

Example

const controller = new AbortController();

setTimeout(function(){
  controller.abort();
},5000);

await fetch(url,{
  signal: controller.signal
});

Common Mistakes

Forgetting to pass the signal.

Wrong:

fetch(url);

Right:

fetch(url);

Reusing a canceled controller.

Remember to create a new controller.

Treating AbortError as a Failure

An aborted request is often expected.

Handle it separately.

Example

if (err.name == "AbortError") {
  return;
}

Browser Support

AbortController is supported by all modern browsers.

It is widely used together with the Fetch API.


AbortController Checklist

  • Create a new controller for each new request
  • Create it with new AbortController()
  • Pass controller.signal to fetch()
  • Call abort() to cancel the request
  • Handle AbortError in a catch block


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->