import { Awaitable } from "./typeUtils"; export async function asyncReduce( arr: T[], callback: (accumulator: V, currentValue: T, index: number, array: T[]) => Awaitable, initialValue?: V, ): Promise { let accumulator; let arrayToIterate; if (initialValue !== undefined) { accumulator = initialValue; arrayToIterate = arr; } else { accumulator = arr[0]; arrayToIterate = arr.slice(1); } for (const [i, currentValue] of arr.entries()) { accumulator = await callback(accumulator, currentValue, i, arr); } return accumulator; } export function asyncFilter( arr: T[], callback: (element: T, index: number, array: T[]) => Awaitable, ): Promise { return asyncReduce( arr, async (newArray, element, i, _arr) => { if (await callback(element, i, _arr)) { newArray.push(element); } return newArray; }, [], ); } export function asyncMap( arr: T[], callback: (currentValue: T, index: number, array: T[]) => Awaitable, ): Promise { return asyncReduce( arr, async (newArray, element, i, _arr) => { newArray.push(await callback(element, i, _arr)); return newArray; }, [], ); }