Iter
takeIterAsync

⚡ Takes the first N elements of an asynchronous iterable.

Syntax

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

Parameters

  • iterable: The source async iterable.
  • n: The number of elements to take. Default: 1.

Returns

An AsyncGenerator that yields the specified number of elements.

Examples

async function* gen() {
  yield 1;
  yield 2;
  yield 3;
}
 
const iter = takeIterAsync(gen(), 2);
for await (const val of iter) {
  console.log(val); //=> 1, 2
}

Related

Try