Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

batch.md

65 lines1.1 KB

1---
2id: batch
3title: batch
4---
5​
6```ts
7function batch<TValue>(fn, options): (item) => void;
8```
9​
10Defined in: [batcher.ts:323](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/batcher.ts#L323)
11​
12Creates a batcher that processes items in batches.
13​
14This synchronous version is lighter weight and often all you need - upgrade to asyncBatch when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
15​
16## Type Parameters
17​
18### TValue
19​
20`TValue`
21​
22## Parameters
23​
24### fn
25​
26(`items`) => `void`
27​
28### options
29​
30[`BatcherOptions`](../interfaces/BatcherOptions.md)\<`TValue`\>
31​
32## Returns
33​
34```ts
35(item): void;
36```
37​
38Adds an item to the batcher
39If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
40​
41### Parameters
42​
43#### item
44​
45`TValue`
46​
47### Returns
48​
49`void`
50​
51## Example
52​
53```ts
54const batchItems = batch<number>(
55 (items) => console.log('Processing:', items),
56 {
57 maxSize: 3,
58 onExecute: (batch, batcher) => console.log('Batch executed:', batch)
59 }
60);
61​
62batchItems(1);
63batchItems(2);
64batchItems(3); // Triggers batch processing
65```