UNPKG

eust-conciliation

Version:
31 lines (24 loc) 981 B
/* eslint-disable no-await-in-loop */ import { Inject, Injectable } from '@severo-tech/injection-decorator'; import { ILogger } from '../contracts/logger'; @Injectable() export class PromisesUtil { @Inject('ZenviaLogger') private readonly logger!: ILogger; // eslint-disable-next-line no-unused-vars public async groupExecute<T, R=any>(list: T[], groupSize: number, func: (el: T) => Promise<R>): Promise<R[]> { this.logger.debug(`"PromisesUtil.groupExecute" - Grouping async executions ${groupSize}-by-${groupSize}`); let step = 0; let result: R[] = []; for (let start = 0; start < list.length; start += groupSize) { step += 1; const end = start + groupSize; this.logger.debug(`- Executing step ${step}: ${start} to ${end}`); const group = list.slice(start, end); const asyncProcess = group.map(func); const data = await Promise.all(asyncProcess); result = result.concat(data); } return result; } }