Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

AbortSignal — cancellation signal

AbortSignal passes a cancellation request to fetch; AbortController initiates cancellation, while the signal exposes its state.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const controller = new AbortController();
const signal = controller.signal;
fetch(url, { signal });
controller.abort(reason);

AbortSignal carries a cancellation request to a supporting operation. Obtain it through controller.signal from an AbortController and pass it as { signal } to fetch. The controller, not the signal, initiates cancellation.

After cancellation, signal.aborted is true. If the promise returned by fetch is still pending, aborting rejects it with the cancellation reason. If the promise has already fulfilled with a Response, aborting cannot reject it retroactively, but it can still interrupt reading the response body. This does not undo server actions.

The example aborts before calling fetch, avoiding network-dependent results. This is a Web API, not an ECMAScript feature; Node 24 in the repository does not establish browser support.

Examples

async function main() {
  const controller = new AbortController();
  const signal = controller.signal;
  const reason = new Error("Stopped");

  controller.abort(reason);

  try {
    await fetch("data:text/plain,hello", { signal });
  } catch (error) {
    console.log(signal.aborted);
    console.log(error === reason);
  }
}

main();

Result

true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources