File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change 1
1
import { useCallback } from 'react' ;
2
2
import useRefMounted from './useRefMounted' ;
3
3
4
- export type UsePromise = ( ) => < T > ( promise : Promise < T > ) => Promise < T > ;
4
+ export type UsePromise = < T > ( ) => ( promise : Promise < T > ) => Promise < T > ;
5
5
6
6
const usePromise : UsePromise = ( ) => {
7
7
const refMounted = useRefMounted ( ) ;
8
- return useCallback ( < T > ( promise : Promise < T > ) : Promise < T > =>
9
- new Promise < T > ( ( resolve , reject ) => {
10
- promise . then ( value => {
8
+ return useCallback ( ( promise : Promise < any > ) =>
9
+ new Promise < any > ( ( resolve , reject ) => {
10
+ const onValue = value => {
11
11
if ( refMounted . current ) {
12
12
resolve ( value ) ;
13
13
}
14
- } , error => {
14
+ } ;
15
+ const onError = error => {
15
16
if ( refMounted . current ) {
16
17
reject ( error ) ;
17
18
}
18
- } )
19
+ } ;
20
+ promise . then ( onValue , onError )
19
21
} ) , [ ] ) ;
20
22
} ;
21
23
You canβt perform that action at this time.
0 commit comments