Skip to main content

pipe

pipe() function

Performs left to right function composition. The first argument can have any value; the remaining arguments must be unary.

Signature:

declare function pipe<T1, R>(a: T1, f1: (a: Awaited<T1>) => R): ReturnPipeType<[T1, R]>;

Example

pipe(
[1, 2, 3, 4, 5],
map(a => a + 10),
filter(a => a % 2 === 0),
toArray,
); // [12, 14]

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

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

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

Try It

see pipe, toAsync, map, filter

Open Source Code