Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.