Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

how to cancel/abort ajax request in axios

Writer Sophia Terry

I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline can be managed by mouse's scroll. App sends ajax request for the actual data after any scroll event. Problem that processing of request at server can be more slow than next scroll event. In this case app can have several (2-3 usually) requests that already is deprecated because user scrolls further. it is a problem because every time at receiving of new data timeline begins redraw. (Because it's reactJS + flux) Because of this, the user sees the movement of the timeline back and forth several times. The easiest way to solve this problem, it just abort previous ajax request as in jQuery. For example:

 $(document).ready( var xhr; var fn = function(){ if(xhr && xhr.readyState != 4){ xhr.abort(); } xhr = $.ajax({ url: 'ajax/progress.ftl', success: function(data) { //do something } }); }; var interval = setInterval(fn, 500);
);

How to cancel/abort requests in axios?

2

10 Answers

Axios does not support canceling requests at the moment. Please see this issue for details.

UPDATE: Cancellation support was added in axios v0.15.

EDIT: The axios cancel token API is based on the withdrawn cancelable promises proposal.

Example:

const cancelTokenSource = axios.CancelToken.source();
axios.get('/user/12345', { cancelToken: cancelTokenSource.token
});
// Cancel request
cancelTokenSource.cancel();
12

Using useEffect hook:

useEffect(() => { const ourRequest = Axios.CancelToken.source() // <-- 1st step const fetchPost = async () => { try { const response = await Axios.get(`endpointURL`, { cancelToken: ourRequest.token, // <-- 2nd step }) console.log(response.data) setPost(response.data) setIsLoading(false) } catch (err) { console.log('There was a problem or request was cancelled.') } } fetchPost() return () => { ourRequest.cancel() // <-- 3rd step }
}, [])

Note: For POST request, pass cancelToken as 3rd argument

Axios.post(`endpointURL`, {data}, { cancelToken: ourRequest.token, // 2nd step
})
3
import React, { Component } from "react";
import axios from "axios";
const CancelToken = axios.CancelToken;
let cancel;
class Abc extends Component { componentDidMount() { this.Api(); } Api() { // Cancel previous request if (cancel !== undefined) { cancel(); } axios.post(URL, reqBody, { cancelToken: new CancelToken(function executor(c) { cancel = c; }), }) .then((response) => { //responce Body }) .catch((error) => { if (axios.isCancel(error)) { console.log("post Request canceled"); } }); } render() { return <h2>cancel Axios Request</h2>; }
}
export default Abc;
1

Typically you want to cancel the previous ajax request and ignore it's coming response, only when a new ajax request of that instance is started, for this purpose, do the following:

Example: getting some comments from API:

// declare an ajax request's cancelToken (globally)
let ajaxRequest = null;
function getComments() { // cancel previous ajax if exists if (ajaxRequest ) { ajaxRequest.cancel(); } // creates a new token for upcomming ajax (overwrite the previous one) ajaxRequest = axios.CancelToken.source(); return axios.get('/api/get-comments', { cancelToken: ajaxRequest.token }).then((response) => { console.log(response.data) }).catch(function(err) { if (axios.isCancel(err)) { console.log('Previous request canceled, new request is send', err.message); } else { // handle error } });
}
1

There is really nice package with few examples of usage called axios-cancel. I've found it very helpful. Here is the link:

const CancelToken = axios.CancelToken; const source = CancelToken.source(); let url = ' axios.get(url, { progress: false, cancelToken: source.token }) .then(resp => { alert('done') }) setTimeout(() => { source.cancel('Operation canceled by the user.'); },'1000')

This is how I did it using promises in node. Pollings stop after making the first request.

 var axios = require('axios'); var CancelToken = axios.CancelToken; var cancel; axios.get(' { cancelToken: new CancelToken( function executor(c) { cancel = c; }) } ).then((response) =>{ cancel(); })

Using cp-axios wrapper you able to abort your requests with three diffent types of the cancellation API:

1. Promise cancallation API (CPromise):

Live browser example

 const cpAxios= require('cp-axios'); const url= ' const chain = cpAxios(url) .timeout(5000) .then(response=> { console.log(`Done: ${JSON.stringify(response.data)}`) }, err => { console.warn(`Request failed: ${err}`) }); setTimeout(() => { chain.cancel(); }, 500);

2. Using AbortController signal API:

 const cpAxios= require('cp-axios'); const CPromise= require('c-promise2'); const url= ' const abortController = new CPromise.AbortController(); const {signal} = abortController; const chain = cpAxios(url, {signal}) .timeout(5000) .then(response=> { console.log(`Done: ${JSON.stringify(response.data)}`) }, err => { console.warn(`Request failed: ${err}`) }); setTimeout(() => { abortController.abort(); }, 500);

3. Using a plain axios cancelToken:

 const cpAxios= require('cp-axios'); const url= ' const source = cpAxios.CancelToken.source(); cpAxios(url, {cancelToken: source.token}) .timeout(5000) .then(response=> { console.log(`Done: ${JSON.stringify(response.data)}`) }, err => { console.warn(`Request failed: ${err}`) }); setTimeout(() => { source.cancel(); }, 500);

4. Usage in a custom React hook (Live Demo):

import React from "react";
import { useAsyncEffect } from "use-async-effect2";
import cpAxios from "cp-axios";
/* Note: the related network request will be aborted as well Check out your network console */
function TestComponent({ url, timeout }) { const [cancel, done, result, err] = useAsyncEffect( function* () { return (yield cpAxios(url).timeout(timeout)).data; }, { states: true, deps: [url] } ); return ( <div> {done ? (err ? err.toString() : JSON.stringify(result)) : "loading..."} <button onClick={cancel} disabled={done}> Cancel async effect (abort request) </button> </div> );
}

Update

Axios v0.22.0+ supports AbortController natively:

const controller = new AbortController();
axios.get('/foo/bar', { signal: controller.signal
}).then(function(response) { //...
});
// cancel the request
controller.abort()
2

Starting from v0.22.0 Axios supports AbortController to cancel requests in fetch API way:

const controller = new AbortController();
axios.get('/foo/bar', { signal: controller.signal
}).then(function(response) { //...
});
// cancel the request
controller.abort()

CancelToken deprecated You can also cancel a request using a CancelToken.

The axios cancel token API is based on the withdrawn cancelable promises proposal.

This API is deprecated since v0.22.0 and shouldn't be used in new projects

You can create a cancel token using the CancelToken.source factory as shown below:

1
import {useState, useEffect} from 'react'
export function useProfileInformation({accessToken}) { const [profileInfo, setProfileInfo] = useState(null) useEffect(() => { const abortController = new AbortController() window .fetch(' { headers: {Authorization: `Bearer ${accessToken}`}, method: 'GET', mode: 'cors', signal: abortController.signal, }) .then(res => res.json()) .then(res => setProfileInfo(res.profileInfo)) return function cancel() { abortController.abort() } }, [accessToken]) return profileInfo
}
// src/app.jsx
import React from 'react'
import {useProfileInformation} from './hooks/useProfileInformation'
export function App({accessToken}) { try { const profileInfo = useProfileInformation({accessToken}) if (profileInfo) { return <h1>Hey, ${profileInfo.name}!</h1> } else { return <h1>Loading Profile Information</h1> } } catch (err) { return <h1>Failed to load profile. Error: {err.message}</h1> }
}
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy