Free ATS Friendly Resume Builder Online

Create Your Resume

Resume Builder

Resume Maker

Resume Templates

Resume PDF Download

Create Your Resume is a free online resume builder that helps job seekers create professional, ATS friendly resumes in minutes. Easily build, customize, and download modern resume templates in PDF format.

Our resume maker is designed for freshers and experienced professionals looking to create job-ready resumes. Choose from multiple resume templates, customize sections, and generate ATS optimized resumes online for free.

Create resumes for IT jobs, software developers, freshers, experienced professionals, managers, and students. This free resume builder supports CV creation, resume PDF download, and online resume editing without signup.

Back to React Js
Lesson 42 of 59

What Is AbortController in JavaScript and React? How to Use It for Cancelling API Calls, Preventing Memory Leaks, and Avoiding Race Conditions

AbortController is a modern JavaScript API used to cancel asynchronous operations such as API requests, making it a critical tool for building stable and high-performance React applications. In real-world apps, users frequently navigate between pages, trigger multiple API calls, or rapidly update inputs like search fields. Without proper cancellation, these actions can lead to memory leaks, race conditions, stale UI updates, and wasted network resources. This guide explains what AbortController is, why it exists, how it works internally, and how to use it correctly in React with Fetch and Axios. It also covers real production scenarios, edge cases, comparisons with alternative approaches, common mistakes, and best practices used in large-scale frontend systems.

Why AbortController Is Important in Modern Web Applications

In real-world React applications, API requests are rarely simple. Users navigate away from pages, type quickly in inputs, refresh data repeatedly, or trigger multiple requests unintentionally.

If these requests are not cancelled properly:

  • Old API responses may override new data
  • State updates may happen after component unmount
  • Memory leaks may occur
  • Network bandwidth is wasted

Large production systems such as complex data-driven platforms rely on AbortController to ensure correctness and stability.

What Is AbortController?

AbortController is a built-in JavaScript API that allows you to cancel ongoing asynchronous operations, most commonly HTTP requests made using Fetch.

It works by creating a controller object that can signal cancellation to any operation that supports aborting.

Key Components

  • AbortController – creates the abort signal
  • signal – passed to the async operation
  • abort() – triggers cancellation

How AbortController Works Internally

When you create an AbortController:

  • A signal object is created
  • The signal is passed to Fetch or another API
  • If abort() is called, the signal notifies the request
  • The request is immediately terminated

The promise is rejected with an AbortError, allowing the application to handle it safely.

Basic AbortController Usage with Fetch


const controller = new AbortController();

fetch("/api/users", {
  signal: controller.signal
})
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => {
    if (err.name === "AbortError") {
      console.log("Request aborted");
    }
  });

// Cancel the request
controller.abort();

This is the foundation for safe API cancellation.

Using AbortController in React with useEffect

Why This Is Necessary

React components unmount frequently due to navigation or conditional rendering. If an API request finishes after unmount, calling setState will cause warnings and unpredictable behavior.

Correct React Pattern


useEffect(() => {
  const controller = new AbortController();

  fetch("/api/users", { signal: controller.signal })
    .then(res => res.json())
    .then(data => setUsers(data))
    .catch(err => {
      if (err.name !== "AbortError") {
        setError(err.message);
      }
    });

  return () => {
    controller.abort();
  };
}, []);

This ensures:

  • No state updates after unmount
  • No memory leaks
  • No stale responses

This pattern is essential in workflows like form submission and preview flows.

AbortController for Preventing Race Conditions

What Is the Race Condition Problem?

Race conditions occur when multiple requests are triggered, and responses arrive in a different order than they were sent.

Real-World Example

  • User types "r"
  • User types "re"
  • User types "rea"

If the first request resolves last, the UI displays incorrect results.

Race Condition Fix Using AbortController


useEffect(() => {
  const controller = new AbortController();

  fetch(`/api/search?q=${query}`, {
    signal: controller.signal
  })
    .then(res => res.json())
    .then(setResults)
    .catch(err => {
      if (err.name !== "AbortError") {
        setError(err.message);
      }
    });

  return () => controller.abort();
}, [query]);

Each new query cancels the previous request, guaranteeing only the latest response updates the UI.

AbortController vs Manual Flags (Comparison)

Approach Reliability Performance Recommended
Boolean flagsLowMediumNo
Request IDsMediumMediumSometimes
AbortControllerHighHighYes

AbortController with Axios (Important Note)

Modern Axios versions support AbortController directly.


const controller = new AbortController();

axios.get("/api/users", {
  signal: controller.signal
});

controller.abort();

This replaces older Axios cancellation APIs, simplifying cancellation logic across applications.

Centralized API layers discussed in frontend architecture guides often standardize AbortController usage.

Common Mistakes Developers Make

  • Creating AbortController outside useEffect
  • Reusing the same controller for multiple requests
  • Not handling AbortError separately
  • Aborting requests unnecessarily

Best Practices & Special Notes

  • Create one AbortController per request
  • Always abort on component unmount
  • Ignore AbortError in UI error states
  • Combine with loading and error handling

Practicing cancellation scenarios using hands-on assessments helps developers internalize correct usage.

Final Takeaway

AbortController is not an optional optimization — it is a core reliability tool for modern React applications. It prevents memory leaks, avoids race conditions, protects application state, and improves performance. Mastering AbortController is a strong indicator of senior-level frontend engineering expertise.