Skip to content

Instantly share code, notes, and snippets.

@jgthms
Forked from bartcis/abort-signals-ondestory.js
Created July 22, 2022 08:15
Show Gist options
  • Save jgthms/951267efd87edd267a8538cee612857a to your computer and use it in GitHub Desktop.
Save jgthms/951267efd87edd267a8538cee612857a 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