Skip to content

Commit 827403e

Browse files
authored
feat(useAsyncState): add executeImmediate with the same type as the promise fn (#4716)
Signed-off-by: davidglezz <davidgg666@gmail.com>
1 parent 317093a commit 827403e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

packages/core/useAsyncState/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,32 @@ const { state, isReady, isLoading } = useAsyncState(
1919
{ id: null },
2020
)
2121
```
22+
23+
### Manually trigger the async function
24+
25+
You can also trigger it manually. This is useful when you want to control when the async function is executed.
26+
27+
```vue
28+
<script setup lang="ts">
29+
import { useAsyncState } from '@vueuse/core'
30+
31+
const { state, execute, executeImmediate } = useAsyncState(action, '', { immediate: false })
32+
33+
async function action(event) {
34+
await new Promise(resolve => setTimeout(resolve, 500))
35+
return `${event.target.textContent} clicked!`
36+
}
37+
</script>
38+
39+
<template>
40+
<p>State: {{ state }}</p>
41+
42+
<button class="button" @click="executeImmediate">
43+
Execute now
44+
</button>
45+
46+
<button class="ml-2 button" @click="event => execute(500, event.target)">
47+
Execute with delay
48+
</button>
49+
</template>
50+
```

packages/core/useAsyncState/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ describe('useAsyncState', () => {
2727
expect(state.value).toBe(2)
2828
})
2929

30+
it('should executeImmediate', async () => {
31+
const { executeImmediate, state } = useAsyncState(p1, 0)
32+
expect(state.value).toBe(0)
33+
await executeImmediate(2)
34+
expect(state.value).toBe(2)
35+
})
36+
3037
it('should work with await', async () => {
3138
const asyncState = useAsyncState(p1, 0, { immediate: true })
3239
expect(asyncState.isLoading.value).toBeTruthy()

packages/core/useAsyncState/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface UseAsyncStateReturnBase<Data, Params extends any[], Shallow ext
88
isLoading: Ref<boolean>
99
error: Ref<unknown>
1010
execute: (delay?: number, ...args: Params) => Promise<Data>
11+
executeImmediate: (...args: Params) => Promise<Data>
1112
}
1213

1314
export type UseAsyncStateReturn<Data, Params extends any[], Shallow extends boolean> =
@@ -16,7 +17,7 @@ export type UseAsyncStateReturn<Data, Params extends any[], Shallow extends bool
1617

1718
export interface UseAsyncStateOptions<Shallow extends boolean, D = any> {
1819
/**
19-
* Delay for executing the promise. In milliseconds.
20+
* Delay for the first execution of the promise when "immediate" is true. In milliseconds.
2021
*
2122
* @default 0
2223
*/
@@ -140,6 +141,7 @@ export function useAsyncState<Data, Params extends any[] = any[], Shallow extend
140141
isLoading,
141142
error,
142143
execute,
144+
executeImmediate: (...args: any[]) => execute(0, ...args),
143145
}
144146

145147
function waitUntilIsLoaded() {

0 commit comments

Comments
 (0)