filter-async.ts 401 B

123456789
  1. /**
  2. * Performs a filter operation where the predicate is an async function returning a Promise.
  3. */
  4. export async function filterAsync<T>(arr: T[], predicate: (item: T, index: number) => Promise<boolean> | boolean): Promise<T[]> {
  5. const results: boolean[] = await Promise.all(
  6. arr.map(async (value, index) => predicate(value, index)),
  7. );
  8. return arr.filter((_, i) => results[i]);
  9. }