@@ -12,6 +12,7 @@ const useAsync = (arg1, arg2) => {
1212 const isMounted = useRef ( true )
1313 const lastArgs = useRef ( undefined )
1414 const lastOptions = useRef ( undefined )
15+ const lastPromise = useRef ( undefined )
1516 const abortController = useRef ( { abort : noop } )
1617
1718 const { devToolsDispatcher } = globalScope . __REACT_ASYNC__
@@ -29,9 +30,10 @@ const useAsync = (arg1, arg2) => {
2930 )
3031
3132 const { debugLabel } = options
32- const getMeta = useCallback ( meta => ( { counter : counter . current , debugLabel, ...meta } ) , [
33- debugLabel ,
34- ] )
33+ const getMeta = useCallback (
34+ meta => ( { counter : counter . current , promise : lastPromise . current , debugLabel, ...meta } ) ,
35+ [ debugLabel ]
36+ )
3537
3638 const setData = useCallback (
3739 ( data , callback = noop ) => {
@@ -72,29 +74,26 @@ const useAsync = (arg1, arg2) => {
7274 abortController . current = new globalScope . AbortController ( )
7375 }
7476 counter . current ++
75- return new Promise ( ( resolve , reject ) => {
77+ return ( lastPromise . current = new Promise ( ( resolve , reject ) => {
7678 if ( ! isMounted . current ) return
7779 const executor = ( ) => promiseFn ( ) . then ( resolve , reject )
7880 dispatch ( { type : actionTypes . start , payload : executor , meta : getMeta ( ) } )
79- } )
81+ } ) )
8082 } ,
8183 [ dispatch , getMeta ]
8284 )
8385
8486 const { promise, promiseFn, initialValue } = options
8587 const load = useCallback ( ( ) => {
86- if ( promise ) {
87- return start ( ( ) => promise ) . then (
88- handleResolve ( counter . current ) ,
89- handleReject ( counter . current )
90- )
91- }
9288 const isPreInitialized = initialValue && counter . current === 0
93- if ( promiseFn && ! isPreInitialized ) {
94- return start ( ( ) => promiseFn ( lastOptions . current , abortController . current ) ) . then (
95- handleResolve ( counter . current ) ,
96- handleReject ( counter . current )
97- )
89+ if ( promise ) {
90+ start ( ( ) => promise )
91+ . then ( handleResolve ( counter . current ) )
92+ . catch ( handleReject ( counter . current ) )
93+ } else if ( promiseFn && ! isPreInitialized ) {
94+ start ( ( ) => promiseFn ( lastOptions . current , abortController . current ) )
95+ . then ( handleResolve ( counter . current ) )
96+ . catch ( handleReject ( counter . current ) )
9897 }
9998 } , [ start , promise , promiseFn , initialValue , handleResolve , handleReject ] )
10099
@@ -103,17 +102,16 @@ const useAsync = (arg1, arg2) => {
103102 ( ...args ) => {
104103 if ( deferFn ) {
105104 lastArgs . current = args
106- return start ( ( ) => deferFn ( args , lastOptions . current , abortController . current ) ) . then (
107- handleResolve ( counter . current ) ,
108- handleReject ( counter . current )
109- )
105+ start ( ( ) => deferFn ( args , lastOptions . current , abortController . current ) )
106+ . then ( handleResolve ( counter . current ) )
107+ . catch ( handleReject ( counter . current ) )
110108 }
111109 } ,
112110 [ start , deferFn , handleResolve , handleReject ]
113111 )
114112
115113 const reload = useCallback ( ( ) => {
116- return lastArgs . current ? run ( ...lastArgs . current ) : load ( )
114+ lastArgs . current ? run ( ...lastArgs . current ) : load ( )
117115 } , [ run , load ] )
118116
119117 const { onCancel } = options
@@ -130,7 +128,9 @@ const useAsync = (arg1, arg2) => {
130128 useEffect ( ( ) => {
131129 if ( watchFn && lastOptions . current && watchFn ( options , lastOptions . current ) ) load ( )
132130 } )
133- useEffect ( ( ) => ( lastOptions . current = options ) && undefined )
131+ useEffect ( ( ) => {
132+ lastOptions . current = options
133+ } )
134134 useEffect ( ( ) => {
135135 if ( counter . current ) cancel ( )
136136 if ( promise || promiseFn ) load ( )
0 commit comments