take<T>(source: Iterable<T> | AsyncIterable<T>,count: number,): AsyncIterableIterator<T>
Takes a specified number of elements from the beginning of an async iterable.
import { take } from "./take.ts"; import { count } from "./infinite.ts"; const iterable = take(count(0, 5), 3); for await (const value of iterable) { console.log(value); }
The above example will print the following 3 lines:
0 5 10
If the iterable is shorter than the specified number, the whole elements are taken.
import { take } from "./take.ts"; async function* gen() { yield "foo"; yield "bar"; yield "baz"; } const iterable = take(gen(), 5); for await (const value of iterable) console.log(value);
The above example will print only 3 elements, because gen() yields only 3
elements:
foo bar baz
AsyncIterableIterator<T>
An async iterable that yields the first count elements
from the source iterable.