Open History to see the last commit
1---
2id: createAsyncBatcher
3title: createAsyncBatcher
4---
5
6```ts
7function createAsyncBatcher<TValue, TSelected>(
8 scope,
9 fn,
10 options?,
11selector?): AlpineAsyncBatcher<TValue, TSelected>;
12```
13
14Defined in: [async-batcher/createAsyncBatcher.ts:97](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/async-batcher/createAsyncBatcher.ts#L97)
15
16Creates and retains the AsyncBatcher for its Alpine owner.
17
18Collects items and processes them together when maxSize, wait, or getShouldExecute triggers a batch. Use addItem to accumulate work and flush to process a partial batch.
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 batch executions that have resulted in errors
34- `executionCount`: Number of batch executions that have been started
35- `failedItems`: Array of items that failed during batch processing
36- `isEmpty`: Whether the batcher has no items to process (items array is empty)
37- `isExecuting`: Whether a batch is currently being processed asynchronously
38- `isPending`: Whether the batcher is waiting for the timeout to trigger batch processing
39- `items`: Array of items currently queued for batch processing
40- `lastResult`: The result from the most recent batch execution
41- `settleCount`: Number of batch executions that have completed (either successfully or with errors)
42- `size`: Number of items currently in the batch queue
43- `status`: Current processing status - 'idle' when not processing, 'pending' when waiting for timeout, 'executing' when processing, 'populated' when items are present, but no wait is configured
44- `successCount`: Number of batch executions that have completed successfully
45- `totalItemsFailed`: Total number of items that have failed processing across all batches
46- `totalItemsProcessed`: Total number of items that have been processed across all batches
47
48## Options and ownership
49
50Pass an options object with property getters or a factory. Top-level properties are read
51reactively; function-valued core options remain callbacks. Local options override provider
52defaults. Updates retain the utility, its store, counters, and pending work.
53Destroying the owning scope calls cancel() and abort().
54onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
55must perform all required cleanup. Use flush() where supported to finish pending work.
56
57## Type Parameters
58
59### TValue
60
61`TValue`
62
63### TSelected
64
65`TSelected` = \{
66\}
67
68## Parameters
69
70### scope
71
72[`PacerScope`](../interfaces/PacerScope.md)
73
74Owner of option updates, subscriptions, and cleanup.
75
76### fn
77
78(`items`) => `Promise`\<`any`\>
79
80Function executed by the utility.
81
82### options?
83
84[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineAsyncBatcherOptions`](../interfaces/AlpineAsyncBatcherOptions.md)\<`TValue`, `TSelected`\>\> = `{}`
85
86Core options or a reactive factory, plus an optional onUnmount callback.
87
88### selector?
89
90(`state`) => `TSelected`
91
92Selects state that updates the owner. Omit to leave selected state empty.
93
94## Returns
95
96[`AlpineAsyncBatcher`](../interfaces/AlpineAsyncBatcher.md)\<`TValue`, `TSelected`\>
97
98The retained utility instance with selected state and child subscriptions.
99
100## Example
101
102```ts
103import { createAsyncBatcher } from '@tanstack/alpine-pacer'
104
105const utility = createAsyncBatcher(
106 scope, async (items: Array<string>) => { console.log(items) },
107 { maxSize: 5, wait: 1000 },
108 (state) => ({ size: state.size }),
109)
110utility.addItem('item')
111// Selected state: utility.state.size
112```