Skip to main content

take

take() function

Returns Iterable/AsyncIterable that taken the first argument l values from iterable

Signature:
declare function take<A>(l: number, iterable: Iterable<A>): IterableIterator<A>;

declare function take<A>(l: number, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;

declare function take<A extends Iterable<unknown> | AsyncIterable<unknown>>(l: number): (iterable: A) => ReturnIterableIteratorType<A>;

Example

const iter = take(2, [0, 1, 2, 3, 4, 5, 6]);
iter.next() // {done:false, value: 0}
iter.next() // {done:false, value: 1}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[0, 1, 2, 3, 4, 5, 6],
take(2),
toArray,
); // [0, 1]

await pipe(
Promise.resolve([0, 1, 2, 3, 4, 5, 6]),
take(2),
toArray,
); // [0, 1]

// with toAsync
await pipe(
[Promise.resolve(0), Promise.resolve(1), Promise.resolve(2),
Promise.resolve(3), Promise.resolve(4), Promise.resolve(5), Promise.resolve(6)],
toAsync,
take(2),
toArray,
); // [0, 1]

Try It

see pipe, toAsync, toArray

Open Source Code