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

Info

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

Syntax

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

Parameters

  • arr: The array to iterate over.

  • cb: An async predicate that returns a boolean.

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

Returns

A Promise that resolves to true if any element satisfied the predicate, else false.

Once an element matches, no further ones are started — though those already running are awaited, since work handed to the callback cannot be recalled.

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

Examples

TypeScript
await someAsync([1, 2, 3], async (n) => n > 2); //=> true

Stop hitting the network as soon as one URL answers:

TypeScript
const anyUp = await someAsync(
  urls,
  async (url) => (await fetch(url)).ok,
  4,
); //=> true
Last updated on
Edit this page