( arr: T[], map: (el: T, index?: number) => Promise<Res>, poolLimit: number, )
| 21 | } |
| 22 | |
| 23 | export const promiseMapPool = async <T, Res>( |
| 24 | arr: T[], |
| 25 | map: (el: T, index?: number) => Promise<Res>, |
| 26 | poolLimit: number, |
| 27 | ): Promise<Res[]> => { |
| 28 | const ret: Promise<Res>[] = [] |
| 29 | const executing: Promise<Res>[] = [] |
| 30 | for (const [index, item] of arr.entries()) { |
| 31 | const p = Promise.resolve().then(() => map(item, index)) |
| 32 | ret.push(p) |
| 33 | |
| 34 | if (poolLimit <= arr.length) { |
| 35 | const e: any = p.then(() => executing.splice(executing.indexOf(e), 1)) |
| 36 | executing.push(e) |
| 37 | if (executing.length >= poolLimit) { |
| 38 | await Promise.race(executing) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return Promise.all(ret) |
| 43 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…