[Vuejs]-How to cancel same requests in Vue js?

0👍

First off, just know CancelToken according to docs:

is deprecated since v0.22.0 and shouldn’t be used in new projects

To use CancelToken, there’s just 3 steps:

  1. create a CancelToken
  2. assign that token to a request
  3. invoke the method on CancelToken
const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

or

const CancelToken = axios.CancelToken;
let cancel;

axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // An executor function receives a cancel function as a parameter
    cancel = c;
  })
});

// cancel the request
cancel();

Docs: https://axios-http.com/docs/cancellation

Leave a comment