Created
July 7, 2020 09:31
-
-
Save nguyenhuucam91/f60cf31b391731f714bbcfce484058ed to your computer and use it in GitHub Desktop.
Cancel axios previous request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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