You're Probably Sleeping on AbortController API.
Published on 3/21/2025

Hello everyone! Ifeoluwa here again. Recently, I came across something in JavaScript that I had completely overlooked before—the AbortController API. If you've ever dealt with stale API requests, unnecessary re-renders, or memory leaks, this little tool might just be your new best friend.
I used to think that once a fetch request was made, it just had to run its course—whether or not the result was still needed (should've known that anything can be done in JS🙈). Turns out, there's a way to cancel requests before they complete, and that’s exactly what AbortController
does. Let me show you why this is so cool and how it has helped me.
What is AbortController?
AbortController is a built-in JavaScript API that allows you to cancel fetch requests (or other asynchronous operations) when they’re no longer needed. It prevents unnecessary network usage and improves performance, especially in apps that rely heavily on API calls.
Here's a quick example:
const controller = new AbortController();
const signal = controller.signal;
fetch("https://api.example.com/data", { signal })
.then(response => response.json())
.then(data => console.log(data))
.catch(err => {
if (err.name === "AbortError") {
console.log("Fetch request was aborted.");
} else {
console.error(err);
}
});
// Cancel the request before it completes
controller.abort();
When controller.abort()
is called, the fetch request stops immediately! No wasted bandwidth, no unnecessary state updates—just clean, efficient requests.
Where This Really Helped Me
Handling Rapid Search Inputs Imagine you're building a search bar that fetches results as the user types. Without
AbortController
, every keystroke triggers a new API call—even if the user hasn’t finished typing! UsingAbortController
, I was able to cancel previous requests before sending new ones, making my search feature way snappier.const controller = new AbortController(); const signal = controller.signal; const handleSearch = (query: string) => { controller.abort(); // Cancel previous request controller = new AbortController(); // Create new controller fetch(`https://api.example.com/search?q=${query}`, { signal }) .then(res => res.json()) .then(data => console.log(data)) .catch(err => { if (err.name !== "AbortError") console.error(err); }); };
This made a huge difference in my app’s responsiveness!
Cleaning Up Requests in React Components I used to get errors when unmounting components that had active fetch requests. That’s because JavaScript doesn’t know the component is gone—it still tries to update the state! Using
AbortController
in auseEffect
cleanup function helped me prevent those errors.useEffect(() => { const controller = new AbortController(); const signal = controller.signal; fetch("https://api.example.com/data", { signal }) .then(res => res.json()) .then(setData) .catch(err => { if (err.name !== "AbortError") console.error(err); }); return () => controller.abort(); // Cleanup function to cancel request }, []);
No more trying to update unmounted components—just smooth, error-free execution!
Why You Should Try It
I’m not saying you must use AbortController
, but if you're working with fetch requests often, it’s definitely worth looking into. It has improved my app's performance, helped me avoid unnecessary API calls, and even prevented memory leaks. If you're building apps that rely on fetching data dynamically, this might be a small change that makes a big difference.
Let me know if you’ve used AbortController
before or if you’ve found other ways to handle request cancellations! I'm still learning, and I'd love to hear your thoughts.
Also, if you enjoyed this, consider subscribing to my newsletter! I share cool things I learn as I grow as a developer, and I’d love to have you along for the ride.