Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nguyenhuucam91/f60cf31b391731f714bbcfce484058ed to your computer and use it in GitHub Desktop.
Save nguyenhuucam91/f60cf31b391731f714bbcfce484058ed to your computer and use it in GitHub Desktop.
Cancel axios previous request
let call;
const once = (config = {}) => {
if (call) {
call.cancel("Only one request allowed at a time.");
}
call = axios.CancelToken.source();
config.cancelToken = call.token
return axios(config);
}
/**
* =========USING
*/
var config = {
method: "get",
url: "/your/url/endpoint",
timeout: 60000
}
once(config)
.then(response => {
// success callback
})
.catch(error => {
// error callback
})
.then(() => {
// do no matter error or success
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment