mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
25 lines
623 B
TypeScript
25 lines
623 B
TypeScript
/* eslint-disable */
|
|
import { useCallback } from 'react';
|
|
import useMountedState from './useMountedState';
|
|
|
|
export type UsePromise = () => <T>(promise: Promise<T>) => Promise<T>;
|
|
|
|
const usePromise: UsePromise = () => {
|
|
const isMounted = useMountedState();
|
|
return useCallback(
|
|
(promise: Promise<any>) =>
|
|
new Promise<any>((resolve, reject) => {
|
|
const onValue = value => {
|
|
isMounted() && resolve(value);
|
|
};
|
|
const onError = error => {
|
|
isMounted() && reject(error);
|
|
};
|
|
promise.then(onValue, onError);
|
|
}),
|
|
[]
|
|
);
|
|
};
|
|
|
|
export default usePromise;
|