@jojoxd/batch
v1.2.0
Published
Generic Async Batch Executor
Downloads
2
Readme
batch
Asynchronous Batch Executor.
accumulates multiple promises, executes once (or multiple times), resolves all promises.
Example
import { BatchQueue } from "@jojoxd/batch";
// Manipulates data
// Could e.g. be a large sql query, or an API request
async function executor(items)
{
return items.map((item) => {
return { item, value: item ** 2 };
});
}
// Find Item from input
async function find(input, data)
{
const obj = data.find((data) => data.item === input);
return obj ? obj.value : undefined;
}
const queue = new BatchQueue({
delay: 200, // Delay in ms
maxBatchSize: 50, // Maximum Batch Size
executor,
find,
});
// From multiple places in your code:
// Can for example be a HTTP route that gets hit often
queue.push(1) // Promise<1 ** 2>
queue.push(2) // Promise<2 ** 2>
queue.push(3) // Promise<3 ** 2>