Open History to see the last commit
1---
2id: createAsyncDebouncer
3title: createAsyncDebouncer
4---
5
6```ts
7function createAsyncDebouncer<TFn, TSelected>(
8 host,
9 fn,
10 options,
11selector?): LitAsyncDebouncer<TFn, TSelected>;
12```
13
14Defined in: [async-debouncer/createAsyncDebouncer.ts:96](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/async-debouncer/createAsyncDebouncer.ts#L96)
15
16Creates and retains the AsyncDebouncer for its Lit owner.
17
18Waits for a quiet period, then executes the latest call. Each new call restarts the trailing timer. Configure leading and trailing edges for search, autosave, or resize handlers.
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(childHost, 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- `canLeadingExecute`: Whether the debouncer can execute on the leading edge of the timeout
34- `errorCount`: Number of function executions that have resulted in errors
35- `isExecuting`: Whether the debounced function is currently executing asynchronously
36- `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
37- `lastArgs`: The arguments from the most recent call to maybeExecute
38- `lastResult`: The result from the most recent successful function execution
39- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
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.
49Disconnecting the host 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.
52Reconnecting the host refreshes options and restores subscriptions to the same instance.
53
54## Type Parameters
55
56### TFn
57
58`TFn` *extends* `AnyAsyncFunction`
59
60### TSelected
61
62`TSelected` = \{
63\}
64
65## Parameters
66
67### host
68
69`ReactiveControllerHost`
70
71Owner of option updates, subscriptions, and cleanup.
72
73### fn
74
75`TFn`
76
77Function executed by the utility.
78
79### options
80
81[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitAsyncDebouncerOptions`](../interfaces/LitAsyncDebouncerOptions.md)\<`TFn`, `TSelected`\>\>
82
83Core options or a reactive factory, plus an optional onUnmount callback.
84
85### selector?
86
87(`state`) => `TSelected`
88
89Selects state that updates the owner. Omit to leave selected state empty.
90
91## Returns
92
93[`LitAsyncDebouncer`](../interfaces/LitAsyncDebouncer.md)\<`TFn`, `TSelected`\>
94
95The retained utility instance with selected state and child subscriptions.
96
97## Example
98
99```ts
100import { createAsyncDebouncer } from '@tanstack/lit-pacer'
101
102const utility = createAsyncDebouncer(
103 this, async (value: string) => { console.log(value) },
104 { wait: 500 },
105 (state) => ({ isPending: state.isPending }),
106)
107utility.maybeExecute('item')
108// Selected state: utility.state.isPending
109```