@jojoxd/batch
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

batch

pipeline status coverage report Latest Release

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>

Readme

Keywords

Package Sidebar

Install

npm i @jojoxd/batch

Weekly Downloads

3

Version

1.2.0

License

MIT

Unpacked Size

21.5 kB

Total Files

8

Last publish

Collaborators

  • bakaxd