Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the difference between timeout and cancelToken? #2074

Closed
xiaobitipao opened this issue Apr 2, 2019 · 4 comments
Closed

What is the difference between timeout and cancelToken? #2074

xiaobitipao opened this issue Apr 2, 2019 · 4 comments

Comments

@xiaobitipao
Copy link

Describe the issue
What is the difference between timeout and cancelToken? Does it mean that I only need to use one of them?

Example Code
use CancelToken

let source = CancelToken.source();
setTimeout(() => {
    source.cancel();
}, 3000);
return axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {
    // My logic
})

use timeout

return axios.get(ip + '/config', {timeout: 3000}).then((result) => {
    // My logic
})

Expected behavior, if applicable
I think the results of the above two pieces of code are the same. Is that so?

Environment:

  • Axios Version [e.g. 0.18.0]
  • OS: [e.g. iOS 12.1.0, OSX 10.13.4]
  • Browser [e.g. Chrome, Safari]
  • Browser Version [e.g. 22]
  • Additional Library Versions [e.g. React 16.7, React Native 0.58.0]

Additional context/Screenshots

@TheNoim
Copy link

TheNoim commented Apr 6, 2019

Timeout and canceltoken have different use cases. Timeout is really just for request timeouts. Canceltoken is used for any other reasone to cancel a request, like an user interaction.

@VigneshRamamoorthy1992
Copy link

@TheNoim @xiaobitipao
Can you share an example on how to cancel a service request on user interaction.

scenario:
user'll be calling the same service asynchronously when a new request for the service is triggered the existing service request should cancel and returns only the new response
Thanks!

@TheNoim
Copy link

TheNoim commented Apr 25, 2019

Simple example. Hopefully it helps https://jsfiddle.net/thenoim/049nfae8/18/

@VigneshRamamoorthy1992
Copy link

@TheNoim
Thanks a lot
it worked !

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants