Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/myrotvorets/promise-chunk

Runs a list of native promises in chunks.
https://github.com/myrotvorets/promise-chunk

batch chunk promise

Last synced: 22 days ago
JSON representation

Runs a list of native promises in chunks.

Awesome Lists containing this project

README

        

# promise-chunk

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=myrotvorets_promise-chunk&metric=alert_status)](https://sonarcloud.io/dashboard?id=myrotvorets_promise-chunk)
[![Build and Test](https://github.com/myrotvorets/promise-chunk/actions/workflows/build.yml/badge.svg)](https://github.com/myrotvorets/promise-chunk/actions/workflows/build.yml)

Runs a list of native promises in chunks.

# Example

```typescript
import promiseChunk from '@myrotvorets/promise-chunk'

function* requestGenerator(): Generator>> {
const ids = ['81e', 'a46', 'SQIzfUkYJ', 'JFPROfGtQ', 'g-gQiPV-_'];
for (const id of ids) {
const url = `https://api.thecatapi.com/v1/images/${id}`;
yield fetch(url).then((response) => response.json() as Promise>);
}
}

async function getCats(): Promise {
const jsons = await promiseChunk(requestGenerator(), 2);
jsons
.filter((item): item is Record => !(item instanceof Error))
.forEach((cat) => console.log(cat.url));
}

getCats().catch((e: unknown) => console.error(e));
```