Skip to content

Commit

Permalink
fix: πŸ› fix TypeScript typings for usePromise()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 15, 2019
1 parent aad368b commit ba0acb6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/usePromise.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {useCallback} from 'react';
import useRefMounted from './useRefMounted';

export type UsePromise = () => <T>(promise: Promise<T>) => Promise<T>;
export type UsePromise = <T>() => (promise: Promise<T>) => Promise<T>;

const usePromise: UsePromise = () => {
const refMounted = useRefMounted();
return useCallback(<T>(promise: Promise<T>): Promise<T> =>
new Promise<T>((resolve, reject) => {
promise.then(value => {
return useCallback((promise: Promise<any>) =>
new Promise<any>((resolve, reject) => {
const onValue = value => {
if (refMounted.current) {
resolve(value);
}
}, error => {
};
const onError = error => {
if (refMounted.current) {
reject(error);
}
})
};
promise.then(onValue, onError)
}), []);
};

Expand Down

0 comments on commit ba0acb6

Please sign in to comment.