Today I Learned
Need to Learn
JavaScript AbortController
Early notes on AbortController: what it solves, where it fits, and what I still need to test.
I started learning AbortController in JavaScript, and this entry is still in progress.
What I understand so far
AbortController lets you cancel async operations that support abort signals, especially fetch.
- You create a controller:
const controller = new AbortController() - You pass its signal:
fetch(url, { signal: controller.signal }) - You cancel with:
controller.abort()
Why this matters
- Avoids wasted network work when a component unmounts.
- Prevents outdated responses from racing and overwriting fresh UI state.
- Makes loading flows more predictable.
Minimal example
useEffect(() => {
const controller = new AbortController()
fetch("/api/data", { signal: controller.signal })
.then((response) => response.json())
.then((data) => setData(data))
.catch((error) => {
if (error.name !== "AbortError") {
setError(error)
}
})
return () => controller.abort()
}, [])
Open questions
- Best patterns when multiple requests happen in sequence.
- Interaction with retries and backoff.
- How this compares with request deduping in data libraries.
I will update this post to Learning and then Learned as I validate concrete examples.
