(total: number, concurrency: number, promiseCreator: Function)
| 45 | // and each remaining Promise will be created once one of the earlier Promises resolves.) This async |
| 46 | // function resolves once all `total` Promises have resolved. |
| 47 | export const timesLimit = async (total: number, concurrency: number, promiseCreator: Function) => { |
| 48 | if (total > 0 && concurrency <= 0) throw new RangeError('concurrency must be positive'); |
| 49 | let next = 0; |
| 50 | const addAnother = () => promiseCreator(next++).finally(() => { |
| 51 | if (next < total) return addAnother(); |
| 52 | }); |
| 53 | const promises = []; |
| 54 | for (let i = 0; i < concurrency && i < total; i++) { |
| 55 | promises.push(addAnother()); |
| 56 | } |
| 57 | await Promise.all(promises); |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * An ordinary Promise except the `resolve` and `reject` executor functions are exposed as |
no test coverage detected