Open History to see the last commit
1---
2id: createAsyncRateLimiter
3title: createAsyncRateLimiter
4---
5
6```ts
7function createAsyncRateLimiter<TFn, TSelected>(
8 scope,
9 fn,
10 options,
11selector?): AlpineAsyncRateLimiter<TFn, TSelected>;
12```
13
14Defined in: [async-rate-limiter/createAsyncRateLimiter.ts:94](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/async-rate-limiter/createAsyncRateLimiter.ts#L94)
15
16Creates and retains the AsyncRateLimiter for its Alpine owner.
17
18Accepts 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.
19
20The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
21Use onSuccess, onError, and onSettled for execution outcomes.
22
23## State and subscriptions
24
25Pass a selector to track only the state consumed by the owner. The default selection is {},
26so utility state changes do not update the owner unless it opts in. Selection uses shallow
27comparison. The raw store remains available for additional subscriptions.
28Use utility.subscribe(childScope, selector) for a child subscription. It returns a getter
29and cleans up with the child without canceling the parent utility.
30
31Available state fields:
32
33- `errorCount`: Number of function executions that have resulted in errors
34- `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
35- `isExceeded`: Whether the rate limiter has exceeded the limit
36- `isExecuting`: Whether the rate-limited function is currently executing asynchronously
37- `lastResult`: The result from the most recent successful function execution
38- `rejectionCount`: Number of function executions that have been rejected due to rate limiting
39- `settleCount`: Number of function executions that have completed (either successfully or with errors)
40- `status`: Current execution status - 'disabled' when not active, 'executing' when executing, 'idle' when not executing, 'exceeded' when rate limit is exceeded
41- `successCount`: Number of function executions that have completed successfully
42- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
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.
49Destroying the owning scope calls 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### scope
67
68[`PacerScope`](../interfaces/PacerScope.md)
69
70Owner of option updates, subscriptions, and cleanup.
71
72### fn
73
74`TFn`
75
76Function executed by the utility.
77
78### options
79
80[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineAsyncRateLimiterOptions`](../interfaces/AlpineAsyncRateLimiterOptions.md)\<`TFn`, `TSelected`\>\>
81
82Core options or a reactive factory, plus an optional onUnmount callback.
83
84### selector?
85
86(`state`) => `TSelected`
87
88Selects state that updates the owner. Omit to leave selected state empty.
89
90## Returns
91
92[`AlpineAsyncRateLimiter`](../interfaces/AlpineAsyncRateLimiter.md)\<`TFn`, `TSelected`\>
93
94The retained utility instance with selected state and child subscriptions.
95
96## Example
97
98```ts
99import { createAsyncRateLimiter } from '@tanstack/alpine-pacer'
100
101const utility = createAsyncRateLimiter(
102 scope, async (value: string) => { console.log(value) },
103 { limit: 5, window: 1000 },
104 (state) => ({ executionCount: state.executionCount }),
105)
106utility.maybeExecute('item')
107// Selected state: utility.state.executionCount
108```