Open History to see the last commit
1---
2id: useAsyncRateLimiter
3title: useAsyncRateLimiter
4---
5
6```ts
7function useAsyncRateLimiter<TFn, TSelected>(
8 fn,
9 options,
10selector?): VueAsyncRateLimiter<TFn, TSelected>;
11```
12
13Defined in: [async-rate-limiter/useAsyncRateLimiter.ts:93](https://github.com/TanStack/pacer/blob/main/packages/vue-pacer/src/async-rate-limiter/useAsyncRateLimiter.ts#L93)
14
15Creates and retains the AsyncRateLimiter for its Vue owner.
16
17Accepts at most a configured number of calls in a fixed or sliding window. Calls beyond the limit are rejected rather than queued. Use the state and timing methods to display capacity and retry timing.
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- `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
34- `isExceeded`: Whether the rate limiter has exceeded the limit
35- `isExecuting`: Whether the rate-limited function is currently executing asynchronously
36- `lastResult`: The result from the most recent successful function execution
37- `rejectionCount`: Number of function executions that have been rejected due to rate limiting
38- `settleCount`: Number of function executions that have completed (either successfully or with errors)
39- `status`: Current execution status - 'disabled' when not active, 'executing' when executing, 'idle' when not executing, 'exceeded' when rate limit is exceeded
40- `successCount`: Number of function executions that have completed successfully
41- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
42
43## Options and ownership
44
45Pass an options object with property getters or a factory. Top-level properties are read
46reactively; function-valued core options remain callbacks. Local options override provider
47defaults. Updates retain the utility, its store, counters, and pending work.
48Disposing the component or effect scope calls abort().
49onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
50must perform all required cleanup. Use flush() where supported to finish pending work.
51
52## Type Parameters
53
54### TFn
55
56`TFn` *extends* `AnyAsyncFunction`
57
58### TSelected
59
60`TSelected` = \{
61\}
62
63## Parameters
64
65### fn
66
67`TFn`
68
69Function executed by the utility.
70
71### options
72
73[`VuePacerOptions`](../type-aliases/VuePacerOptions.md)\<[`VueAsyncRateLimiterOptions`](../interfaces/VueAsyncRateLimiterOptions.md)\<`TFn`, `TSelected`\>\>
74
75Core options or a reactive factory, plus an optional onUnmount callback.
76
77### selector?
78
79(`state`) => `TSelected`
80
81Selects state that updates the owner. Omit to leave selected state empty.
82
83## Returns
84
85[`VueAsyncRateLimiter`](../interfaces/VueAsyncRateLimiter.md)\<`TFn`, `TSelected`\>
86
87The retained utility instance with selected state and child subscriptions.
88
89## Example
90
91```ts
92import { useAsyncRateLimiter } from '@tanstack/vue-pacer'
93
94const utility = useAsyncRateLimiter(
95 async (value: string) => { console.log(value) },
96 { limit: 5, window: 1000 },
97 (state) => ({ executionCount: state.executionCount }),
98)
99utility.maybeExecute('item')
100// Selected state: utility.state.value.executionCount
101```