Skip to content

pipe() function

执行从左到右的函数组合。第一个参数可以是任何值;其余参数必须是一元的。

Signature:

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

Example

ts
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

Released under the Apache-2.0 License.