Axios v1 request cancellation and CancelToken deprecation boundary
0 reputation · 13 Feb 2022, 13:44 UTC
0 reputation · 13 Feb 2022, 13:44 UTC
Axios v1.0 introduced the AbortController API for request cancellation, officially deprecating the legacy CancelToken mechanism. While the documentation indicates that CancelToken will be removed in a future major release, there is currently no specific timeline or official migration guide provided to facilitate the transition.
This creates a technical uncertainty for codebases that must maintain compatibility across varying browser and Node.js environments. AbortController is natively supported in modern browsers, but Node.js environments prior to v18.13.0 lack native support within the http.Agent. Without a standardized mapping of legacy CancelToken usage patterns to the new AbortController implementation, developers face inconsistent cancellation handling logic across cross-platform projects.
What is the recommended strategy for structuring new axios v1 implementations to ensure future-proofing without breaking legacy environment support? How should developers handle the discrepancy in AbortController availability between older Node.js versions and modern browsers? Is there a documented pattern for wrapping complex CancelToken logic to be AbortController compatible?
35275 reputation · 13 Feb 2022, 15:31 UTC
Use AbortController (the signal option) for all new Axios requests. For Node.js <18.13.0, import the abort-controller polyfill and expose it as a global so Axios can use the same API. Wrap legacy CancelToken code in a small shim that forwards abort events to an AbortController, but keep the shim out of new code paths. This keeps modern browsers and older Node environments consistent, and the shim can be removed once the code base no longer uses CancelToken.
AbortSignal via the signal config property.DeprecationWarning and will be removed in Axios v2.AbortController.abort-controller npm package works in Node <18.13.0 and is fully compatible with Axios.signal as authoritative; if both signal and cancelToken are supplied, signal wins.npm install abort-controller --save
if (typeof AbortController === 'undefined') {
const { AbortController } = require('abort-controller');
global.AbortController = AbortController;
}
function createAbortConfig() {
const controller = new AbortController();
return {
signal: controller.signal,
abort: () => controller.abort(),
};
}
const { signal, abort } = createAbortConfig();
axios.get('/api/data', { signal })
.then(handleSuccess)
.catch(err => {
if (axios.isCancel(err)) return console.log('Request cancelled');
throw err;
});
// When needed:
abort();
CancelToken, wrap it so that it forwards to an AbortController:
function legacyToAbortToken() {
const controller = new AbortController();
const token = new axios.CancelToken(cancel => {
controller.signal.addEventListener('abort', () => cancel('canceled'));
});
return { token, abort: () => controller.abort() };
}
cancelToken usage with the new signal approach. Once all requests use signal, remove the shim.
AbortSignal.any (Node 16.14+ / modern browsers). For older environments, implement a small combinator:
function anySignal(signals) {
const controller = new AbortController();
signals.forEach(s => s.addEventListener('abort', () => controller.abort()));
return controller.signal;
}
const combinedSignal = anySignal([uiSignal, timeoutSignal]);
axios.get('/api', { signal: combinedSignal });
axios.isCancel returns true.
DeprecationWarning appears when signal is used.
Do you currently use any Axios interceptors or adapters that still reference config.cancelToken? If so, those will need to be audited before fully dropping CancelToken.
Some projects keep a dual‑path approach: they expose an AbortController polyfill for Node <18, then use a shim to translate legacy CancelToken calls into AbortSignal events. This allows a gradual migration while keeping the code base functional across all target environments. Once the project no longer relies on CancelToken, the shim can be removed, leaving a single, future‑proof cancellation strategy.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.