An open API service indexing awesome lists of open source software.

https://github.com/edvardchen/batch-dispatch

A small library for async batching process
https://github.com/edvardchen/batch-dispatch

Last synced: about 1 year ago
JSON representation

A small library for async batching process

Awesome Lists containing this project

README

          

# batch-dispatch

A small toolkit to allow batch-processing scattering async calls. Inspired by GraphQL's DataLoader

## Install

```bash
npm i batch-dispatch
```

## Usage

```js
import { batchDispatch } from "batch-dispatch";

const getUserInfoById = batchDispatch(
(paramsList) => {
return fetch(url, {
method: "POST",
body: JSON.stringify({
list: paramsList.map((params) => params[0]),
}),
});
},
(result, params) => {
return result.list.find((item) => item.id === params[0]);
},
);

getUserInfoById(1).then((user) => {
// user.id === 1
});

getUserInfoById(2).then((user) => {
// user.id === 2
});
```