es-toolkit/docs/reference/function/flow.md

49 lines
1.3 KiB
Markdown
Raw Normal View History

2024-09-26 10:35:41 +03:00
# flow
Creates a function that returns the result of invoking the given functions with the `this` binding of the created function, where each successive invocation is supplied the return value of the previous.
## Signature
```typescript
function flow<R>(f: () => R): () => R;
function flow<A extends any[], R>(f1: (...args: A) => R): (...args: A) => R;
function flow<A extends any[], R1, R2>(f1: (...args: A) => R1, f2: (a: R1) => R2): (...args: A) => R2;
function flow<A extends any[], R1, R2, R3>(
f1: (...args: A) => R1,
f2: (a: R1) => R2,
f3: (a: R2) => R3
): (...args: A) => R3;
function flow<A extends any[], R1, R2, R3, R4>(
f1: (...args: A) => R1,
f2: (a: R1) => R2,
f3: (a: R2) => R3,
f4: (a: R3) => R4
): (...args: A) => R4;
function flow<A extends any[], R1, R2, R3, R4, R5>(
f1: (...args: A) => R1,
f2: (a: R1) => R2,
f3: (a: R2) => R3,
f4: (a: R3) => R4,
f5: (a: R4) => R5
): (...args: A) => R5;
function flow(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;
```
### Parameters
- `funcs` (`Array<(...args: any[]) => any>`): The functions to invoke.
### Returns
(`(...args: any[]) => any`): The new composite function.
## Examples
```typescript
const add = (x: number, y: number) => x + y;
const square = (n: number) => n * n;
const combined = flow(add, square);
console.log(combined(1, 2)); // => 9
```