๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€

โš ๏ธ When working with frontend applications, API requests can pile up, especially with features like live search or auto-suggestions.
โš ๏ธ If you donโ€™t cancel outdated requests, you waste bandwidth and slow down your app.
โš ๏ธ Outdated requests can sometime…


This content originally appeared on DEV Community and was authored by Dzung Nguyen

โš ๏ธ When working with frontend applications, API requests can pile up, especially with features like live search or auto-suggestions.
โš ๏ธ If you donโ€™t cancel outdated requests, you waste bandwidth and slow down your app.
โš ๏ธ Outdated requests can sometimes cause flickering UI

โค๏ธ If you are using Axios to make API requests, here are the two ways you can do:
โœ… Use AbortController (For versions > 0.22)
๐Ÿ‘‰ AbortController provides a signal that Axios listens to.
๐Ÿ‘‰ When abort() is called, it immediately stops the request, preventing unnecessary responses from being processed.

import axios from "axios";

const controller = new AbortController();
const signal = controller.signal;

axios.get("https://api.example.com/data", { signal })
  .then(response => console.log(response.data))
  .catch(error => {
    if (error.name === "AbortError") {
      console.log("Request was canceled");
    } else {
      console.error("Request failed", error);
    }
  });

// Cancel the request
controller.abort();

โœ… Use CancelToken (For versions < 0.22)
๐Ÿ‘‰ CancelToken creates a cancelable request by passing a token.
๐Ÿ‘‰ The token acts as a reference to the request, allowing Axios to identify and cancel it when needed.
๐Ÿ‘‰ When cancel() is called on the token, Axios recognizes the signal and aborts the associated request, preventing unnecessary processing.

import axios from "axios";

const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get("https://api.example.com/data", { cancelToken: source.token })
  .then(response => console.log(response.data))
  .catch(error => {
    if (axios.isCancel(error)) {
      console.log("Request was canceled:", error.message);
    } else {
      console.error("Request failed", error);
    }
  });

// Cancel the request
source.cancel("Operation canceled by the user.");

Follow me to stay updated with my future posts:


This content originally appeared on DEV Community and was authored by Dzung Nguyen


Print Share Comment Cite Upload Translate Updates
APA

Dzung Nguyen | Sciencx (2025-02-15T14:37:48+00:00) ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€. Retrieved from https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/

MLA
" » ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€." Dzung Nguyen | Sciencx - Saturday February 15, 2025, https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/
HARVARD
Dzung Nguyen | Sciencx Saturday February 15, 2025 » ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€., viewed ,<https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/>
VANCOUVER
Dzung Nguyen | Sciencx - » ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/
CHICAGO
" » ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€." Dzung Nguyen | Sciencx - Accessed . https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/
IEEE
" » ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€." Dzung Nguyen | Sciencx [Online]. Available: https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» ๐Ÿš€ Efficient API Calls with Axios: Cancelling Unwanted Requests ๐Ÿš€ | Dzung Nguyen | Sciencx | https://www.scien.cx/2025/02/15/%f0%9f%9a%80-efficient-api-calls-with-axios-cancelling-unwanted-requests-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.