Skip to content

Instantly share code, notes, and snippets.

@finom
Created May 11, 2020 13:40
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 finom/f97a6a4f344690a2486668bb3e207e38 to your computer and use it in GitHub Desktop.
Save finom/f97a6a4f344690a2486668bb3e207e38 to your computer and use it in GitHub Desktop.
[finom/use-promise-effect] A promise effect hook
/*
Usage:
usePromiseEffect(async => () => {
await fetch(foo);
// ...
})
*/
import { useEffect } from 'react';
export default function usePromiseEffect(callback, memos) {
return useEffect(() => {
Promise.resolve(callback()).catch((e) => { throw e; });
}, memos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment