(
fns: (() => Promise<any>)[],
options: ExecAsyncOptions = ExecAsyncSchema.parse({}),
)
| 15 | * @param options Options |
| 16 | */ |
| 17 | export async function execAsync( |
| 18 | fns: (() => Promise<any>)[], |
| 19 | options: ExecAsyncOptions = ExecAsyncSchema.parse({}), |
| 20 | ) { |
| 21 | const limit = pLimit(options.concurrency); |
| 22 | const limitedFns = fns.map((fn) => () => limit(fn)); |
| 23 | |
| 24 | const resultPromises: Promise<any>[] = []; |
| 25 | |
| 26 | let completedCount = 0; |
| 27 | options.onProgress?.(completedCount, limitedFns.length); |
| 28 | |
| 29 | for (let i = 0; i < limitedFns.length; i++) { |
| 30 | const fn = limitedFns[i]; |
| 31 | const resultPromise = fn().then((result) => { |
| 32 | completedCount++; |
| 33 | options.onProgress?.(completedCount, limitedFns.length); |
| 34 | return result; |
| 35 | }); |
| 36 | resultPromises.push(resultPromise); |
| 37 | |
| 38 | await Promise.race([resultPromise, delay(options.delay)]); |
| 39 | } |
| 40 | |
| 41 | const results = await Promise.all(resultPromises); |
| 42 | return results; |
| 43 | } |
| 44 | |
| 45 | export type ExecWithRetryOptions = Z.infer<typeof ExecWithRetrySchema>; |
| 46 |
no test coverage detected