Concurrency
filterAsync

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

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

Syntax

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.

Examples

const items = [1, 2, 3, 4];
const result = await filterAsync(items, async (n) => {
  return n % 2 === 0;
}); //=> [2, 4]

Related

Try