Skip to content

Instantly share code, notes, and snippets.

@amirrezaDev1378
Created September 3, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrezaDev1378/9334b1ca7864145a0902803e2debef21 to your computer and use it in GitHub Desktop.
Save amirrezaDev1378/9334b1ca7864145a0902803e2debef21 to your computer and use it in GitHub Desktop.
function useAsync(asyncFn, onSuccess) {
useEffect(() => {
let isActive = true;
asyncFn().then(data => {
if (isActive) onSuccess(data);
});
return () => { isActive = false };
}, [asyncFn, onSuccess]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment