Open History to see the last commit
1---
2id: useAsyncThrottler
3title: useAsyncThrottler
4---
5
6```ts
7function useAsyncThrottler<TFn, TSelected>(
8 fn,
9 options,
10selector?): VueAsyncThrottler<TFn, TSelected>;
11```
12
13Defined in: [async-throttler/useAsyncThrottler.ts:94](https://github.com/TanStack/pacer/blob/main/packages/vue-pacer/src/async-throttler/useAsyncThrottler.ts#L94)
14
15Creates and retains the AsyncThrottler for its Vue owner.
16
17Limits execution to at most one call per wait interval. Leading and trailing options control immediate and deferred execution; the trailing call uses the latest arguments.
18
19The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
20Use onSuccess, onError, and onSettled for execution outcomes.
21
22## State and subscriptions
23
24Pass a selector to track only the state consumed by the owner. The default selection is {},
25so utility state changes do not update the owner unless it opts in. Selection uses shallow
26comparison. The raw store remains available for additional subscriptions.
27Read selected state through utility.state.value in JavaScript. Vue templates unwrap refs.
28Use utility.Subscribe with a scoped slot to select state in a child without updating the owner.
29
30Available state fields:
31
32- `errorCount`: Number of function executions that have resulted in errors
33- `isExecuting`: Whether the throttled function is currently executing asynchronously
34- `isPending`: Whether the throttler is waiting for the timeout to trigger execution
35- `lastArgs`: The arguments from the most recent call to maybeExecute
36- `lastExecutionTime`: Timestamp of the last function execution in milliseconds
37- `lastResult`: The result from the most recent successful function execution
38- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
39- `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
40- `settleCount`: Number of function executions that have completed (either successfully or with errors)
41- `status`: Current execution status - 'idle' when not active, 'pending' when waiting, 'executing' when running, 'settled' when completed
42- `successCount`: Number of function executions that have completed successfully
43
44## Options and ownership
45
46Pass an options object with property getters or a factory. Top-level properties are read
47reactively; function-valued core options remain callbacks. Local options override provider
48defaults. Updates retain the utility, its store, counters, and pending work.
49Disposing the component or effect scope calls cancel() and abort().
50onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
51must perform all required cleanup. Use flush() where supported to finish pending work.
52
53## Type Parameters
54
55### TFn
56
57`TFn` *extends* `AnyAsyncFunction`
58
59### TSelected
60
61`TSelected` = \{
62\}
63
64## Parameters
65
66### fn
67
68`TFn`
69
70Function executed by the utility.
71
72### options
73
74[`VuePacerOptions`](../type-aliases/VuePacerOptions.md)\<[`VueAsyncThrottlerOptions`](../interfaces/VueAsyncThrottlerOptions.md)\<`TFn`, `TSelected`\>\>
75
76Core options or a reactive factory, plus an optional onUnmount callback.
77
78### selector?
79
80(`state`) => `TSelected`
81
82Selects state that updates the owner. Omit to leave selected state empty.
83
84## Returns
85
86[`VueAsyncThrottler`](../interfaces/VueAsyncThrottler.md)\<`TFn`, `TSelected`\>
87
88The retained utility instance with selected state and child subscriptions.
89
90## Example
91
92```ts
93import { useAsyncThrottler } from '@tanstack/vue-pacer'
94
95const utility = useAsyncThrottler(
96 async (value: string) => { console.log(value) },
97 { wait: 500 },
98 (state) => ({ isPending: state.isPending }),
99)
100utility.maybeExecute('item')
101// Selected state: utility.state.value.isPending
102```