@zemnmez/future 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# `@zemnmez/future`
|
|
2
2
|
|
|
3
|
+
[View `@zemnmez/future` on npm](https://www.npmjs.com/package/@zemnmez/future)
|
|
4
|
+
|
|
3
5
|
`@zemnmez/future` is a tiny TypeScript type for a value that may be loading,
|
|
4
6
|
resolved, or errored. A `Future<Then, Loading, Error>` represents all three
|
|
5
7
|
states exactly and requires each state to be handled before its resolved value
|
|
@@ -74,4 +76,49 @@ const message = future(
|
|
|
74
76
|
types. `future_flatten_then` flattens nested Futures, and `future_collect`
|
|
75
77
|
combines several Futures into one.
|
|
76
78
|
|
|
79
|
+
## Pipelines
|
|
80
|
+
|
|
81
|
+
`future_and_then` and `future_flatten_then` compose Future-returning operations
|
|
82
|
+
into a pipeline. Each operation receives the preceding resolved value, while a
|
|
83
|
+
loading or error state stops the pipeline:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import {
|
|
87
|
+
error,
|
|
88
|
+
Future,
|
|
89
|
+
future_and_then,
|
|
90
|
+
future_flatten_then,
|
|
91
|
+
resolve,
|
|
92
|
+
} from '@zemnmez/future';
|
|
93
|
+
|
|
94
|
+
interface User {
|
|
95
|
+
id: number;
|
|
96
|
+
name: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const findUser = (
|
|
100
|
+
id: number
|
|
101
|
+
): Future<User, 'fetching user', 'user not found'> =>
|
|
102
|
+
id === 42
|
|
103
|
+
? resolve({ id, name: 'Deep Thought' })
|
|
104
|
+
: error('user not found');
|
|
105
|
+
|
|
106
|
+
const displayName = (
|
|
107
|
+
user: User
|
|
108
|
+
): Future<string, 'formatting name', 'missing name'> =>
|
|
109
|
+
user.name ? resolve(user.name) : error('missing name');
|
|
110
|
+
|
|
111
|
+
const user = future_flatten_then(
|
|
112
|
+
future_and_then(resolve(42), findUser)
|
|
113
|
+
);
|
|
114
|
+
const name = future_flatten_then(
|
|
115
|
+
future_and_then(user, displayName)
|
|
116
|
+
);
|
|
117
|
+
// Future<
|
|
118
|
+
// string,
|
|
119
|
+
// 'fetching user' | 'formatting name',
|
|
120
|
+
// 'user not found' | 'missing name'
|
|
121
|
+
// >
|
|
122
|
+
```
|
|
123
|
+
|
|
77
124
|
[result]: https://www.npmjs.com/package/@zemnmez/result
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"version": "1.0.
|
|
21
|
+
"version": "1.0.2",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@tanstack/react-query": "5.101.4",
|
|
24
24
|
"react": "19.2.8"
|
|
@@ -33,6 +33,6 @@
|
|
|
33
33
|
"directory": "ts/future"
|
|
34
34
|
},
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/zemn-me/monorepo/issues/new?title=%2F%2Fts%2Ffuture%401.0.
|
|
36
|
+
"url": "https://github.com/zemn-me/monorepo/issues/new?title=%2F%2Fts%2Ffuture%401.0.2%3A+something+went+wrong%21"
|
|
37
37
|
}
|
|
38
38
|
}
|