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
- Host: GitHub
- URL: https://github.com/edvardchen/batch-dispatch
- Owner: edvardchen
- License: mit
- Created: 2024-08-02T08:59:24.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-08T12:52:20.000Z (almost 2 years ago)
- Last Synced: 2025-05-01T19:03:42.891Z (about 1 year ago)
- Language: TypeScript
- Size: 35.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
});
```