npm.io
1.1.0 • Published 2 years ago

promise-partial

Licence
MIT
Version
1.1.0
Deps
0
Size
6 kB
Vulns
0
Weekly
0
Stars
1

Promise partial

Version Size Build

Partial (mixed) promise execution

.

Partial

Array is divided on groups by K items. Items in groups is handled in parallel. But groups are called in turn.

await promisePartial(items, someAsyncFunction, K)

.

For example - Default methods:

.

Serial

Each item of array is handled one by one. Like a simple for

for (const value of items) {
    await someAsyncFunction(value)
}

.

Parallel

Each item of array is handled in parallel. Like a Promise.all

await Promise.all(items.map(someAsyncFunction))

.

npm i promise-partial
promisePartial<T, D>(
    // Array of items for map
    array: T[],
    // Callback for handle of item
    callback: (item: T, index: number) => Promise<D>,
    // Part size for array dividing
    partSize: number
): Promise<D>[]:
import promisePartial from 'promise-partial';

const items = [1, 2, 3, /* and more items */];
const partSize = 2;

const result = await promisePartial(items, async (value) => {
    return new Promise((resolve) => {
        // some async process
        setTimeout(() => resolve(value * 2), 100);
    });
}, partSize);

Keywords