Batch promises together in style. packaged as @jojoxd/batch
Find a file
semantic-release-bot 87263f54fe chore(release): 1.2.0 [skip ci]
# [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 ([ee6148b](ee6148b0e6))

### Features

* **queue:** Add index to queue ([0b374fd](0b374fd00a))
2022-11-21 11:00:14 +00:00
.yarn/releases Initial Commit 2022-03-21 17:03:51 +01:00
dist feat(queue): Add index to queue 2022-03-23 21:30:01 +01:00
examples feat(queue): Add index to BatchQueue.find() parameter 2022-03-23 21:25:44 +01:00
scripts/mocha Initial Commit 2022-03-21 17:03:51 +01:00
src feat(queue): Add index to queue 2022-03-23 21:30:01 +01:00
.gitignore Initial Commit 2022-03-21 17:03:51 +01:00
.gitlab-ci.yml fix(build): Add caching for yarn deps, add junit reporter artifact 2022-03-21 17:25:00 +01:00
.mocha-mmr.json Initial Commit 2022-03-21 17:03:51 +01:00
.mocharc.cjs Initial Commit 2022-03-21 17:03:51 +01:00
.npmignore Initial Commit 2022-03-21 17:03:51 +01:00
.nycrc Initial Commit 2022-03-21 17:03:51 +01:00
.releaserc fix(release): Fix .releaserc not committing newer versions 2022-03-23 21:33:07 +01:00
.yarnrc.yml Initial Commit 2022-03-21 17:03:51 +01:00
example.ts Initial Commit 2022-03-21 17:03:51 +01:00
index.ts Initial Commit 2022-03-21 17:03:51 +01:00
LICENSE.md Initial Commit 2022-03-21 17:03:51 +01:00
package.json chore(release): 1.2.0 [skip ci] 2022-11-21 11:00:14 +00:00
README.md feat(queue): Add index to queue 2022-03-23 21:30:01 +01:00
renovate.json chore(deps): add renovate.json 2022-11-21 10:48:06 +00:00
tsconfig.json feat(queue): Add index to queue 2022-03-23 21:30:01 +01:00
yarn.lock Initial Commit 2022-03-21 17:03:51 +01:00

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>