question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cancelling group by using "axios.default.cancelToken = ..." causes "config.cancelToken.throwIfRequested"

See original GitHub issue

#### Summary

I am running axios requests in tests and when the test logic is complete I want to cancel all active requests (which are not related to the logic of the test). This is my init code:

let CancelToken = axios.CancelToken;
let axiosSource = CancelToken.source();
export const axiosCancel = axiosSource.cancel;
axios.defaults.cancelToken = axiosSource.token;

and then, (using mocha) I run cancel like this:

afterEach(() => {
  axiosCancel();
});

When axiosCancel() is run I get this error in the stack:

Error: TypeError: config.cancelToken.throwIfRequested is not a function
    at Error (native)
    at /Users/Ben_Katz/Git/wix/user-manager/src/client/actions/fetchUserByDomain.ts:34:13 } with Error: TypeError: config.cancelToken.throwIfRequested is not a function
    at Error (native)
    at /Users/Ben_Katz/Git/wix/user-manager/src/client/actions/fetchUserByDomain.ts:34:13 +0ms

I don’t know if this is not working because it’s not supposed to work that way but the error gives me no information and as far as I can tell it’s a bug. #### Context

  • axios version: v0.16.3*
  • Environment: node v6.10.4, MacOs sierra*

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Bnayacommented, Jan 14, 2018

A fast workaround for this issue:

...
const CancelToken = Axios.CancelToken;
this.source = CancelToken.source();
// OMG! AXIOS has ISSUES! it merges the cancel token incorrectly, so we trick it and move it from the prototype to the object itself
this.source.token.throwIfRequested = this.source.token.throwIfRequested;
this.source.token.promise.then = this.source.token.promise.then.bind(this.source.token.promise);
this.source.token.promise.catch = this.source.token.promise.catch.bind(this.source.token.promise);
...
0reactions
ru-web-designercommented, Jan 24, 2018

@Bnaya got it, thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cancelling group by using "axios.default ... - Bountysource
Cancelling group by using "axios.default.cancelToken = ..." causes "config.cancelToken.throwIfRequested" ... I am running axios requests in tests ...
Read more >
Axios cancel token cancelling request before even called
I hope you should throttle your requests instead of canceling the request. Could you please try the following if throttle does not suit...
Read more >
Cancellation | Axios Docs
CancelToken deprecated. You can also cancel a request using a CancelToken. The axios cancel token API is based on the withdrawn cancelable promises...
Read more >
How to use the axios.CancelToken function in axios - Snyk
CancelToken examples, based on popular ways it is used in public projects. ... Cancellation) => { let cancellable = true return { cancelToken:...
Read more >
Axios Cancel Request - Morioh
The Axios global has a CancelToken object that can create a cancel token for ... passing a reason for the cancellation, you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found