Skip to content

pipe

Creates a left‑to‑right transformation pipeline. Each function receives the output of the previous one. Supports both synchronous and asynchronous functions and guarantees that it never mutates input and never throws. If a function throws synchronously, the pipeline returns a rejected Promise instead of propagating the throw.

Use pipe when you want a predictable, intention‑revealing sequence of transformations.

Signature

function pipe<T>(value: T): T;
function pipe<T, A>(value: T, fn1: (v: T) => A | Promise<A>): A | Promise<A>;
function pipe<T, A, B>(
  value: T,
  fn1: (v: T) => A | Promise<A>,
  fn2: (v: A) => B | Promise<B>
): B | Promise<B>;
// Additional overloads for longer pipelines…

Parameters

Returns

One of:

  • T — if no functions are provided.
  • The final transformed value — if all functions are synchronous and none throw.
  • A Promise resolving to the final value — if any function is asynchronous.
  • A Promise rejecting with an error — if any function throws synchronously or returns a rejected Promise.

Behavior

  • Pure function: no side effects.
  • Never mutates input and never throws.
  • Synchronous throws inside pipeline functions are converted into rejected Promises.
  • Applies functions left‑to‑right.
  • If no functions are provided, returns the initial value unchanged.
  • If any function returns a Promise, the entire pipeline becomes asynchronous.
  • If a function throws, the pipeline becomes asynchronous and returns a rejected Promise.

Examples

pipe(2, n => n + 1, n => n * 2)
// 6

await pipe(3, n => n + 1, async n => n * 2)
// 8

const bad = () => {
  throw new Error("fail");
};

await pipe(1, bad)
// Promise.reject(Error("fail"))

No functions
pipe("hello")
// "hello"

Notes

  • Preferred way to express sequential transformations in Jane’s functional helpers.
  • It is intentionally minimal: no branching, no cleverness, no mutation.
  • Because it never throws, it is safe to use inside validation, normalization, and result pipelines.
  • For right‑to‑left composition, use compose.
  • Works seamlessly with tap, maybe, and tryOrDefault.