many-promises
many-promises allows sequential or parallel promise evaluation, including limiting number of promises that can be in progress at the same time. Includes Typescript typings, but can be used in Javascript projects as well.
Installation
npm install many-promises
Example
; const items = 1 2 3 4 5 6 7 8 9 10; const job = Promise; const results = await ;// Will call `job` for every `item` in `items` and await until all promises are resolved.// results - [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
// All promises in parallel:const results = await ;// All promises sequentially:const results = await ;// Maximum 3 jobs at the same time, but each job has a minimum 200ms duration:const results = await ;
Reference
execute(options: ExecuteOptions)
;
Note: Types ItemType and ResultType are usually inferred and you don't need to specify them explicitly.
items
Array of items of any type.
job
A function with one argument - which is an item from the items
array.
concurrentLimit
(optional)
Maximum number of Promises that can be in progress at the same time.
Default - undefined
minJobTime
(optional)
Minimum time of execution for one job. If a job finishes earlier, next job will wait for this timeout to end before starting.
Default - undefined