Skip to content

Commit 2936dc6

Browse files
committed
feat: add compose function for composing multiple operators
1 parent bcf44d9 commit 2936dc6

File tree

8 files changed

+1268
-0
lines changed

8 files changed

+1268
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ const result = pipe(
4747
console.log(result); // "4"
4848
```
4949

50+
If you want to create a new function that composes multiple operators, use
51+
`compose` like below.
52+
53+
```ts
54+
import { compose } from "@core/pipe/compose";
55+
56+
const operator = compose(
57+
(v: number) => v + 1, // The first operator must be typed explicitly
58+
(v) => v * 2, // inferred as (v: number) => number
59+
(v) => v.toString(), // inferred as (v: number) => string
60+
);
61+
console.log(operator(1)); // "4"
62+
```
63+
64+
Or use `async` module to compose multiple asynchronous operators.
65+
66+
```ts
67+
import { compose } from "@core/pipe/async/compose";
68+
69+
const operator = compose(
70+
(v: number) => Promise.resolve(v + 1), // The first operator must be typed explicitly
71+
(v) => Promise.resolve(v * 2), // inferred as (v: number) => number | Promise<number>
72+
(v) => Promise.resolve(v.toString()), // inferred as (v: number) => string | Promise<string>
73+
);
74+
console.log(await operator(1)); // "4"
75+
```
76+
5077
## Difference
5178

5279
The `pipe` function in the root module is equivalent to function calls without

0 commit comments

Comments
 (0)