Batch promises together in style. packaged as @jojoxd/batch
# [1.2.0](https://gitlab.jojoxd.nl/jojoxd-npm/batch/compare/v1.1.0...v1.2.0) (2022-11-21) ### Bug Fixes * **release:** Fix .releaserc not committing newer versions ([ |
||
|---|---|---|
| .yarn/releases | ||
| dist | ||
| examples | ||
| scripts/mocha | ||
| src | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| .mocha-mmr.json | ||
| .mocharc.cjs | ||
| .npmignore | ||
| .nycrc | ||
| .releaserc | ||
| .yarnrc.yml | ||
| example.ts | ||
| index.ts | ||
| LICENSE.md | ||
| package.json | ||
| README.md | ||
| renovate.json | ||
| tsconfig.json | ||
| yarn.lock | ||
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>