Skip to content

myrotvorets/promise-chunk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1066ff2 · May 11, 2021

History

78 Commits
Apr 22, 2021
Oct 28, 2020
Oct 24, 2020
Oct 24, 2020
Oct 24, 2020
Oct 24, 2020
Feb 22, 2021
Oct 24, 2020
Oct 24, 2020
Oct 24, 2020
Oct 24, 2020
Apr 10, 2021
May 11, 2021
May 11, 2021
Apr 20, 2021
Oct 24, 2020

Repository files navigation

promise-chunk

Quality Gate Status Build and Test CI codebeat badge

Runs a list of native promises in chunks.

Example

import promiseChunk from '@myrotvorets/promise-chunk'

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

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

getCats().catch((e) => console.error(e));