0
0
Node.jsframework~10 mins

AbortController for cancellation in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - AbortController for cancellation
Create AbortController
Start async task with signal
Task running...
AbortController.abort() called?
NoTask completes normally
Task receives abort signal
Task cancels early
Handle task result or cancellation
This flow shows how an AbortController creates a signal to cancel an async task early if needed.
Execution Sample
Node.js
const controller = new AbortController();
const signal = controller.signal;

fetch('https://example.com', { signal })
  .then(() => console.log('Success'))
  .catch(e => console.log('Aborted:', e.name));

controller.abort();
Starts a fetch request with an abort signal, then cancels it immediately.
Execution Table
StepActionSignal StateTask StateOutput
1Create AbortControllernot abortednot started
2Start fetch with signalnot abortedrunning
3AbortController.abort() calledabortedrunning
4Fetch detects abort signalabortedcancelled
5Fetch promise rejectsabortedcancelledAborted: AbortError
6Catch block runsabortedcancelledAborted: AbortError
💡 Fetch is cancelled because abort() was called, causing the promise to reject with AbortError.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
controller.signal.abortedfalsefalsetruetrue
fetch task statenot startedrunningrunningcancelled
Key Moments - 3 Insights
Why does the fetch promise reject after calling controller.abort()?
Because the abort signal changes to aborted (see Step 3 in execution_table), the fetch detects this and rejects with an AbortError (Step 5).
Does calling abort() immediately stop the fetch function?
No, abort() signals cancellation but the fetch promise rejects asynchronously after detecting the signal (Steps 3 to 5).
What happens if abort() is never called?
The fetch runs normally and resolves or rejects based on network response, without cancellation (Step 2 runs to completion).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the signal.aborted value after Step 3?
Aundefined
Bfalse
Ctrue
Dnull
💡 Hint
Check the 'Signal State' column at Step 3 in the execution_table.
At which step does the fetch task change from running to cancelled?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Task State' column in the execution_table to see when it changes.
If controller.abort() was not called, what would happen to the fetch task state?
AIt would stay running forever
BIt would complete normally
CIt would cancel automatically
DIt would throw an error immediately
💡 Hint
Refer to the key_moments explanation about what happens if abort() is never called.
Concept Snapshot
AbortController creates a signal to cancel async tasks.
Pass signal to async function (like fetch).
Call abort() to set signal.aborted = true.
Async task detects signal and cancels early.
Promise rejects with AbortError on cancellation.
Full Transcript
AbortController is a tool in Node.js to cancel asynchronous operations early. You create an AbortController instance, then get its signal property. This signal is passed to async functions like fetch. When you call abort() on the controller, the signal's aborted property becomes true. The async function notices this and stops its work, rejecting its promise with an AbortError. This lets you handle cancellations cleanly. The execution flow starts with creating the controller, then starting the async task with the signal. If abort() is called, the task detects the signal and cancels. Otherwise, it runs to completion. Variables like signal.aborted and the task state change step-by-step as shown in the execution table. Understanding when and how abort() affects the task helps avoid confusion about asynchronous cancellation.