⚡ Asynchronous version of `Array.prototype.filter`.

Info

By default, it runs all iterations in parallel. You can limit the concurrency by providing a third argument.

Syntax

TypeScript
import { filterAsync } from '@opentf/std';

filterAsync<T>(
  arr: T[],
  cb: (value: T, index: number) => Promise<boolean>,
  concurrency?: number
): Promise<T[]>;

Parameters

  • arr: The array to iterate over.

  • cb: An async callback function that returns a boolean.

  • concurrency (Optional): The maximum number of concurrent executions. Default: Infinity.

Returns

A Promise that resolves to the filtered array.

Sparse array holes are skipped, matching native Array.prototype.filter() behavior.

Examples

TypeScript
const items = [1, 2, 3, 4];
const result = await filterAsync(items, async (n) => {
  return n % 2 === 0;
}); //=> [2, 4]
Last updated on
Edit this page