🏃‍♂️ Yields the first N elements of an async iterable.

Syntax

TypeScript
import { takeIterAsync } from '@opentf/std';
takeIterAsync<T>(iterable: AsyncIterable<T>, n: number): AsyncGenerator<T>

Parameters

  • iterable: The source async iterable.

  • n: The number of items to take.

Returns

A new async generator with the first n items, or all of them if the source is shorter.

Exactly n items are read from the source and no more, so a value the result does not include is never awaited — which matters when producing one costs a request. An n of zero or less reads nothing at all.

Examples

TypeScript
async function* gen() { yield 1; yield 2; yield 3; }
await toArrayIterAsync(takeIterAsync(gen(), 2)) //=> [1, 2]
Last updated on
Edit this page