Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created January 20, 2020 20:10
Show Gist options
  • Save bartcis/00137d09549e667c8b2b5710725ba012 to your computer and use it in GitHub Desktop.
Save bartcis/00137d09549e667c8b2b5710725ba012 to your computer and use it in GitHub Desktop.
Example aborting of multiple signals on destroy
useEffect(() => {
const abortController = new AbortController();
const {signal} = abortController;
const apiCall = async path => {
try {
const request = await fetch(path, {
signal: signal,
method: 'GET',
});
const response = await request.json();
setState([response]);
} catch (e) {
if (!signal?.aborted) {
console.error(e);
}
}
};
const abortClickRequests = () => {
abortFuncs.current.map(abort => abort());
}
apiCall('https://jsonplaceholder.typicode.com/posts/1');
return () => {
abortClickRequests();
abortController.abort();
};
}, [setState]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment