filterAsync

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

Info

By default, it runs all iterations in parallel. You can limit the concurrency through an options object.

Syntax

TypeScript
import { filterAsync } from '@opentf/std';
filterAsync<T>(
  arr: T[],
  cb: (value: T, index: number) => Promise<boolean>,
  options?: { concurrency?: number }
): Promise<T[]>;

Parameters

  • arr: The array to iterate over.

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

  • options.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]

const limited = await filterAsync(items, async (n) => n % 2 === 0, {
  concurrency: 2,
}); //=> [2, 4]

Try it

Last updated on
Edit this page