promise-partial
v1.1.0
Published
Partial (mixed) promise execution
Downloads
41
Maintainers
Readme
⚡ Promise partial
Partial (mixed) promise execution
.
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:
.
Each item of array is handled one by one. Like a simple for
for (const value of items) {
await someAsyncFunction(value)
}
.
Each item of array is handled in parallel. Like a Promise.all
await Promise.all(items.map(someAsyncFunction))
.
Install
npm i promise-partial
Usage
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>[]:
Example
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);