Cancelling Operations with AbortController
Cancelling Operations with AbortController — the core idea
AbortController creates a signal that cooperative APIs can use to stop pending work. Aborting does not roll back a completed side effect, and an API must support the signal for cancellation to work. fetch can receive the signal via its options.
What you’ll learn
- Explain cancelling operations with abortcontroller with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The controller is aborted before fetch begins, so fetch rejects for the aborted signal. The catch block identifies the error name AbortError. No network request is needed.
Keep the boundary clear: Aborting a request does not guarantee that a server operation was undone; design idempotent or safely retryable server actions separately.
See the idea as a flow
Run the complete TypeScript example
Save the code as cancelling-operations-abortcontroller.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
async function main(): Promise<void> {
const controller = new AbortController();
controller.abort();
try {
await fetch("data:text/plain,hello", { signal: controller.signal });
} catch (error: unknown) {
if (error instanceof Error) console.log(error.name);
}
}
main().catch(console.error);Verified output
AbortErrorBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable cancelling-operations-abortcontroller.ts; run: node cancelling-operations-abortcontroller.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The controller is aborted before fetch begins, so fetch rejects for the aborted signal. The catch block identifies the error name AbortError. No network request is needed.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Aborting a request does not guarantee that a server operation was undone; design idempotent or safely retryable server actions separately.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Create a controller, pass its signal to an operation, and trigger abort from a timeout, clearing the timeout when work finishes.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
AbortController creates a signal that cooperative APIs can use to stop pending work.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.