Last active
February 15, 2020 19:24
-
-
Save vtenfys/8583b4b0da14e408818e65c9a5d0b0df to your computer and use it in GitHub Desktop.
usePromise React Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react"; | |
const promiseMap = new WeakMap(); | |
function usePromise(promise) { | |
let data = {}; | |
if (promiseMap.has(promise)) { | |
data = promiseMap.get(promise); | |
} else { | |
promiseMap.set(promise, data); | |
} | |
useEffect(() => { | |
// cleanup: free map memory | |
return () => { | |
promiseMap.delete(promise); | |
}; | |
}, [promise]); | |
data.status = data.status ?? "pending"; | |
if (data.status === "pending") { | |
data.suspender = | |
data.suspender ?? | |
promise | |
.then(result => { | |
data.status = "success"; | |
data.result = result; | |
}) | |
.catch(error => { | |
data.status = "error"; | |
data.result = error; | |
}); | |
throw data.suspender; | |
} | |
if (data.status === "error") { | |
throw data.result; | |
} | |
return data.result; | |
} | |
export default usePromise; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Combine this with a Suspense fallback and define a promise outside of your component to wait until that promise is resolved. E.g.: