diff --git a/packages/core/useAsyncState/index.test.ts b/packages/core/useAsyncState/index.test.ts index cae4e009d14..9d20304c062 100644 --- a/packages/core/useAsyncState/index.test.ts +++ b/packages/core/useAsyncState/index.test.ts @@ -27,6 +27,14 @@ describe('useAsyncState', () => { expect(state.value).toBe(2) }) + it('should work with initialized', async () => { + const { execute, initialized, isLoading } = useAsyncState(p1, 0, { immediate: false }) + execute() + expect(isLoading.value).toBeTruthy() + await initialized.value + expect(isLoading.value).toBeFalsy() + }) + it('should work with isLoading', () => { const { execute, isLoading } = useAsyncState(p1, 0, { immediate: false }) expect(isLoading.value).toBeFalsy() diff --git a/packages/core/useAsyncState/index.ts b/packages/core/useAsyncState/index.ts index 726d471eba4..45a02dae56e 100644 --- a/packages/core/useAsyncState/index.ts +++ b/packages/core/useAsyncState/index.ts @@ -5,6 +5,7 @@ import { ref, shallowRef } from 'vue-demi' export interface UseAsyncStateReturn { state: Shallow extends true ? Ref : Ref> isReady: Ref + initialized: Ref | undefined> isLoading: Ref error: Ref execute: (delay?: number, ...args: Params) => Promise @@ -92,6 +93,8 @@ export function useAsyncState(undefined) + let initialize: CallableFunction + const initialized = shallowRef | undefined>(undefined) async function execute(delay = 0, ...args: any[]) { if (resetOnExecute) @@ -99,6 +102,9 @@ export function useAsyncState { + initialize = resolve + }) if (delay > 0) await promiseTimeout(delay) @@ -111,6 +117,7 @@ export function useAsyncState : Ref>, isReady, + initialized, isLoading, error, execute,