Defined in: async-batcher.ts:229
A class that collects items and processes them in batches asynchronously.
This is the async version of the Batcher class. Unlike the sync version, this async batcher:
Batching is a technique for grouping multiple operations together to be processed as a single unit.
The AsyncBatcher provides a flexible way to implement async batching with configurable:
Error Handling:
State Management:
const batcher = new AsyncBatcher<number>(
async (items) => {
const result = await processItems(items);
console.log('Processing batch:', items);
return result;
},
{
maxSize: 5,
wait: 2000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed and the result will be available
// batcher.execute() // manually trigger a batch
const batcher = new AsyncBatcher<number>(
async (items) => {
const result = await processItems(items);
console.log('Processing batch:', items);
return result;
},
{
maxSize: 5,
wait: 2000,
onSuccess: (result) => console.log('Batch succeeded:', result),
onError: (error) => console.error('Batch failed:', error)
}
);
batcher.addItem(1);
batcher.addItem(2);
// After 2 seconds or when 5 items are added, whichever comes first,
// the batch will be processed and the result will be available
// batcher.execute() // manually trigger a batch
• TValue
new AsyncBatcher<TValue>(fn, initialOptions): AsyncBatcher<TValue>
new AsyncBatcher<TValue>(fn, initialOptions): AsyncBatcher<TValue>
Defined in: async-batcher.ts:236
(items) => Promise<any>
AsyncBatcherOptions<TValue>
AsyncBatcher<TValue>
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: async-batcher.ts:233
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
Defined in: async-batcher.ts:230
addItem(item): void
addItem(item): void
Defined in: async-batcher.ts:287
Adds an item to the async batcher If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
TValue
void
clear(): void
clear(): void
Defined in: async-batcher.ts:407
Removes all items from the async batcher
void
flush(): Promise<any>
flush(): Promise<any>
Defined in: async-batcher.ts:363
Processes the current batch of items immediately
Promise<any>
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: async-batcher.ts:389
Returns a copy of all items in the async batcher
TValue[]
peekFailedItems(): TValue[]
peekFailedItems(): TValue[]
Defined in: async-batcher.ts:393
TValue[]
reset(): void
reset(): void
Defined in: async-batcher.ts:414
Resets the async batcher state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-batcher.ts:251
Updates the async batcher options
Partial<AsyncBatcherOptions<TValue>>
void
start(): void
start(): void
Defined in: async-batcher.ts:379
Starts the async batcher and processes any pending items
void
stop(): void
stop(): void
Defined in: async-batcher.ts:371
Stops the async batcher from processing batches
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.