/* * © Copyright 2022 HP Development Company, L.P. * SPDX-License-Identifier: MIT */ type Iterable = (item: T, index: number) => Promise | ReturnType; export async function mapSeries( data: T[], fn: Iterable ): Promise { const results = []; for (let index = 0; index < data.length; index++) { const item = data[index]; // eslint-disable-next-line results.push(await fn(item, index)); } return results; } export async function mapParallel( data: T[], fn: Iterable ): Promise { return Promise.all(data.map((item, index) => fn(item, index))); } export async function nextTick) => any>(fn?: FN): Promise> { return new Promise((resolve, reject) => { process.nextTick(async () => { try { resolve(await fn?.()); } catch (err) { reject(err); } }); }); }