( callback: () => Promise<R>, deps: DependencyList )
| 38 | * ``` |
| 39 | */ |
| 40 | export const useAsyncValue = <R>( |
| 41 | callback: () => Promise<R>, |
| 42 | deps: DependencyList |
| 43 | ): AsyncValueHookResult<R> => { |
| 44 | const [ asyncCallback, asyncState ] = useAsyncCallback( callback ); |
| 45 | |
| 46 | useInstantEffect( asyncCallback, deps ); |
| 47 | |
| 48 | // There might be short delay between the effect and the state update. |
| 49 | // So it is possible that the status is still 'idle' after the effect. |
| 50 | // In such case, we should return 'loading' status because the effect is already queued to be executed. |
| 51 | if ( asyncState.status === 'idle' ) { |
| 52 | return { |
| 53 | status: 'loading' |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | return asyncState; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * The result of the `useAsyncValue` hook. |
no test coverage detected
searching dependent graphs…