Skip to main content

prepend

prepend() function

Returns Iterable/AsyncIterable with the given element at the front, followed by the contents of iterable.

Signature:
declare function prepend<A>(a: A, iterable: Iterable<A>): IterableIterator<A>;

declare function prepend<A>(a: A | Promise<A>, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;

declare function prepend<A, B extends Iterable<A> | AsyncIterable<Awaited<A>>>(a: A): (iterable: B) => ReturnPrependType<A, B>;

Example

const iter = prepend(4, [1, 2, 3]);
iter.next() // {done:false, value: 4}
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 2}
iter.next() // {done:false, value: 3}

// with pipe
pipe(
[1, 2, 3],
prepend(4),
toArray,
); // [4, 1, 2, 3]

await pipe(
Promise.resolve([1, 2, 3]),
prepend(4),
toArray,
); // [4, 1, 2, 3]

// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)],
toAsync,
prepend(4),
toArray,
); // [4, 1, 2, 3]

Try It

see pipe, toAsync, toArray