JavaScript: abort()

Calling controller.abort() cancels a fetch request or other operation connected to its signal.

What you will learn

Minimal example

const controller = new AbortController();
fetch("data.json", { signal: controller.signal })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => {
    if (error.name === "AbortError") {
      console.log("The request was cancelled.");
      return;
    }
    console.error("The request failed:", error);
  });

controller.abort();

Pass controller.signal when starting the operation. Calling abort() later rejects the Promise with an AbortError, so cancellation can be reported separately from a network or server failure.

Important points