Defined in: async-batcher.ts:230
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:238
(items) => Promise<any>
AsyncBatcherOptions<TValue>
AsyncBatcher<TValue>
fn: (items) => Promise<any>;
fn: (items) => Promise<any>;
Defined in: async-batcher.ts:239
TValue[]
Promise<any>
key: string;
key: string;
Defined in: async-batcher.ts:234
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
Defined in: async-batcher.ts:235
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
Defined in: async-batcher.ts:231
addItem(item): void
addItem(item): void
Defined in: async-batcher.ts:297
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:398
Removes all items from the async batcher
void
flush(): Promise<any>
flush(): Promise<any>
Defined in: async-batcher.ts:372
Processes the current batch of items immediately
Promise<any>
peekAllItems(): TValue[]
peekAllItems(): TValue[]
Defined in: async-batcher.ts:380
Returns a copy of all items in the async batcher
TValue[]
peekFailedItems(): TValue[]
peekFailedItems(): TValue[]
Defined in: async-batcher.ts:384
TValue[]
reset(): void
reset(): void
Defined in: async-batcher.ts:405
Resets the async batcher state to its default values
void
setOptions(newOptions): void
setOptions(newOptions): void
Defined in: async-batcher.ts:260
Updates the async batcher options
Partial<AsyncBatcherOptions<TValue>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.