Skip to content

Instantly share code, notes, and snippets.

@alanhe421
Created February 9, 2020 08:33
Show Gist options
  • Save alanhe421/52d2a8a04e4695d158ec61ba7977cf3f to your computer and use it in GitHub Desktop.
Save alanhe421/52d2a8a04e4695d158ec61ba7977cf3f to your computer and use it in GitHub Desktop.
import axios from 'axios';
/**
* Cancel previous pending request
*/
const pending = new Map();
export const addPending = config => {
const url = [config.method, config.url].join('&');
config.cancelToken =
config.cancelToken ||
new axios.CancelToken(cancel => {
if (!pending.has(url)) {
pending.set(url, cancel);
}
});
};
export const removePending = config => {
const url = [config.method, config.url].join('&');
if (pending.has(url)) {
const cancel = pending.get(url);
cancel(url);
pending.delete(url);
}
};
export const clearPending = () => {
for (const [url, cancel] of pending) {
cancel(url);
}
pending.clear();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment