11import { useState , useEffect , useMemo } from "react"
22
3- const useAsync = props => {
3+ const useAsync = ( opts , init ) => {
44 let counter = 0
55 let isMounted = false
66 let lastArgs = undefined
77
8- const initialError = props . initialValue instanceof Error ? props . initialValue : undefined
9- const initialData = initialError ? undefined : props . initialValue
10- const [ data , setData ] = useState ( initialData )
11- const [ error , setError ] = useState ( initialError )
12- const [ startedAt , setStartedAt ] = useState ( props . promiseFn ? new Date ( ) : undefined )
13- const [ finishedAt , setFinishedAt ] = useState ( props . initialValue ? new Date ( ) : undefined )
8+ const options = typeof opts === "function" ? { promiseFn : opts , initialValue : init } : opts
9+ const { promiseFn, deferFn, initialValue, onResolve, onReject, watch } = options
10+
11+ const [ data , setData ] = useState ( initialValue instanceof Error ? undefined : initialValue )
12+ const [ error , setError ] = useState ( initialValue instanceof Error ? initialValue : undefined )
13+ const [ startedAt , setStartedAt ] = useState ( promiseFn ? new Date ( ) : undefined )
14+ const [ finishedAt , setFinishedAt ] = useState ( initialValue ? new Date ( ) : undefined )
1415
1516 const cancel = ( ) => {
1617 counter ++
@@ -44,21 +45,21 @@ const useAsync = props => {
4445 return error
4546 }
4647
47- const onResolve = count => data => count === counter && handleData ( data , props . onResolve )
48- const onReject = count => error => count === counter && handleError ( error , props . onReject )
48+ const handleResolve = count => data => count === counter && handleData ( data , onResolve )
49+ const handleReject = count => error => count === counter && handleError ( error , onReject )
4950
5051 const load = ( ) => {
51- if ( props . promiseFn ) {
52+ if ( promiseFn ) {
5253 start ( )
53- props . promiseFn ( props ) . then ( onResolve ( counter ) , onReject ( counter ) )
54+ promiseFn ( options ) . then ( handleResolve ( counter ) , handleReject ( counter ) )
5455 }
5556 }
5657
5758 const run = ( ...args ) => {
58- if ( props . deferFn ) {
59+ if ( deferFn ) {
5960 lastArgs = args
6061 start ( )
61- return props . deferFn ( ...args , props ) . then ( onResolve ( counter ) , onReject ( counter ) )
62+ return deferFn ( ...args , options ) . then ( handleResolve ( counter ) , handleReject ( counter ) )
6263 }
6364 }
6465
@@ -69,7 +70,7 @@ const useAsync = props => {
6970 return ( ) => ( isMounted = false )
7071 } , [ ] )
7172
72- useEffect ( load , [ props . promiseFn , props . watch ] )
73+ useEffect ( load , [ promiseFn , watch ] )
7374
7475 return useMemo (
7576 ( ) => ( {
@@ -78,12 +79,12 @@ const useAsync = props => {
7879 finishedAt,
7980 data,
8081 error,
82+ initialValue,
8183 cancel,
8284 run,
8385 reload,
8486 setData : handleData ,
85- setError : handleError ,
86- initialValue : props . initialValue
87+ setError : handleError
8788 } ) ,
8889 [ data , error , startedAt , finishedAt ]
8990 )
0 commit comments