@@ -27,6 +27,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
2727 this . setData = this . setData . bind ( this )
2828 this . setError = this . setError . bind ( this )
2929
30+ const promise = props . promise
3031 const promiseFn = props . promiseFn || defaultProps . promiseFn
3132 const initialValue = props . initialValue || defaultProps . initialValue
3233 const initialError = initialValue instanceof Error ? initialValue : undefined
@@ -40,8 +41,8 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
4041 initialValue,
4142 data : initialData ,
4243 error : initialError ,
43- isLoading : ! initialValue && isFunction ( promiseFn ) ,
44- startedAt : undefined ,
44+ isLoading : ! ! promise || ( promiseFn && ! initialValue ) ,
45+ startedAt : promise ? new Date ( ) : undefined ,
4546 finishedAt : initialValue ? new Date ( ) : undefined ,
4647 counter : this . counter ,
4748 cancel : this . cancel ,
@@ -57,14 +58,20 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
5758
5859 componentDidMount ( ) {
5960 this . mounted = true
60- this . state . initialValue || this . load ( )
61+ if ( this . props . promise || ! this . state . initialValue ) {
62+ this . load ( )
63+ }
6164 }
6265
6366 componentDidUpdate ( prevProps ) {
64- const { watch, watchFn = defaultProps . watchFn , promiseFn } = this . props
67+ const { watch, watchFn = defaultProps . watchFn , promise , promiseFn } = this . props
6568 if ( watch !== prevProps . watch ) this . load ( )
6669 if ( watchFn && watchFn ( { ...defaultProps , ...this . props } , { ...defaultProps , ...prevProps } ) )
6770 this . load ( )
71+ if ( promise !== prevProps . promise ) {
72+ if ( promise ) this . load ( )
73+ else this . cancel ( )
74+ }
6875 if ( promiseFn !== prevProps . promiseFn ) {
6976 if ( promiseFn ) this . load ( )
7077 else this . cancel ( )
@@ -91,24 +98,32 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
9198 }
9299
93100 load ( ) {
101+ const promise = this . props . promise
102+ if ( promise ) {
103+ this . start ( )
104+ return promise . then ( this . onResolve ( this . counter ) , this . onReject ( this . counter ) )
105+ }
106+
94107 const promiseFn = this . props . promiseFn || defaultProps . promiseFn
95- if ( ! promiseFn ) return
96- this . start ( )
97- return promiseFn ( this . props , this . abortController ) . then (
98- this . onResolve ( this . counter ) ,
99- this . onReject ( this . counter )
100- )
108+ if ( promiseFn ) {
109+ this . start ( )
110+ return promiseFn ( this . props , this . abortController ) . then (
111+ this . onResolve ( this . counter ) ,
112+ this . onReject ( this . counter )
113+ )
114+ }
101115 }
102116
103117 run ( ...args ) {
104118 const deferFn = this . props . deferFn || defaultProps . deferFn
105- if ( ! deferFn ) return
106- this . args = args
107- this . start ( )
108- return deferFn ( args , { ...defaultProps , ...this . props } , this . abortController ) . then (
109- this . onResolve ( this . counter ) ,
110- this . onReject ( this . counter )
111- )
119+ if ( deferFn ) {
120+ this . args = args
121+ this . start ( )
122+ return deferFn ( args , { ...defaultProps , ...this . props } , this . abortController ) . then (
123+ this . onResolve ( this . counter ) ,
124+ this . onReject ( this . counter )
125+ )
126+ }
112127 }
113128
114129 cancel ( ) {
@@ -162,6 +177,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
162177 if ( PropTypes ) {
163178 Async . propTypes = {
164179 children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) ,
180+ promise : PropTypes . instanceOf ( Promise ) ,
165181 promiseFn : PropTypes . func ,
166182 deferFn : PropTypes . func ,
167183 watch : PropTypes . any ,
0 commit comments