Skip to content

Instantly share code, notes, and snippets.

@JonathonAshworth
Last active August 9, 2022 03:00
Show Gist options
  • Save JonathonAshworth/291694336cd48c02610570310c33fd07 to your computer and use it in GitHub Desktop.
Save JonathonAshworth/291694336cd48c02610570310c33fd07 to your computer and use it in GitHub Desktop.
Cleaning up asynchronous render side effects
useEffect(() => {
const pFoo = getFoo().then(foo => /* do somthing with our async foo, making sure to return it */);
return () => pFoo.then(foo => /* cleanup here will always run after we have gotten our foo and used it */);
});
// This is one of the few cases where it makes sense to use raw promises instead of async/await
// The power of promises is that you can compose asynchronous logic asynchronously
// Afaik that's not possible with async/await?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment