⚡ Promise partial
Partial (mixed) promise execution
.
Array is divided on grave of group by K items. Items in groups is handled in parallel. But groups themselves are called in turn
await promisePartial(items, someAsyncFunction, K)
.
For example - other 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 = 1000
): Promise<D>[]:
-
Example
const promisePartial = require('promise-partial');
const res = await promisePartial([1, 2, 3, /* and more items */], async (v) => {
return new Promise((resolve) => {
// some async process
setTimeout(() => resolve(v * 2), 100);
});
});