import { useCallback } from 'react' ; import useMountedState from './useMountedState' ; export type UsePromise = ( ) => < T > ( promise : Promise < T > ) => Promise < T > ; const usePromise = ( ) => { const isMounted = useMountedState ( ) ; return useCallback ( ( promise ) => new Promise < any > ( ( resolve , reject ) => { const onValue = ( value ) => { isMounted ( ) && resolve ( value ) ; } ; const onError = ( error ) => { isMounted ( ) && reject ( error ) ; } ; promise . then ( onValue , onError ) ; } ) , [ ] ) ; } ; export default usePromise ;