Skip to main content

map

map() function

Returns Iterable/AsyncIterable of values by running each applying f.

Signature:
declare function map<A, B>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<B>;

declare function map<A, B>(f: (a: A) => B, iterable: AsyncIterable<A>): AsyncIterableIterator<Awaited<B>>;

declare function map<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(f: (a: IterableInfer<A>) => B): (iterable: A) => ReturnIterableIteratorType<A, B>;

Example

const iter = map(a => a + 10, [1, 2, 3, 4]);
iter.next() // {done:false, value: 11}
iter.next() // {done:false, value: 12}
iter.next() // {done:false, value: 13}
iter.next() // {done:false, value: 14},
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[1, 2, 3, 4],
map(a => a + 10),
toArray,
); // [11, 12, 13, 14]

await pipe(
Promise.resolve([1, 2, 3, 4]),
map(a => a + 10),
toArray,
); // [11, 12, 13, 14]

// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4]),
toAsync,
map(async (a) => a + 10),
toArray,
); // [11, 12, 13, 14]

// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4)],
toAsync,
map(a => a + 10),
toArray,
); // [11, 12, 13, 14]

Try It

see pipe, toAsync, toArray