You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below are the monads included in this package and examples of their use (more examples of all monads and their methods can be found in the library unit tests).
17
-
18
-
### Maybe
14
+
## How to Use
19
15
20
-
`Maybe` represents a value that might or might not exist.
`Maybe` represents a value that might or might not exist. You can use it to declaratively describe a process (series of steps) without having to check if there is a value present.
none(() =>console.log(`The business process did not return any employees`))
89
+
});
137
90
```
138
91
139
-
#### Pipe
92
+
1.`tryFirst` finds the first employee in the array and wraps it in a `Maybe`. If the array is empty, a `Maybe` with no value is returned.
93
+
1.`tap`'s callback is only called if an employee was found and logs out that employee's information.
94
+
1.`bind`'s callback is only called if an employee was found and converts the `Maybe` wrapping it into to another `Maybe`.
95
+
1.`from` wraps the employee's manager in a `Maybe`. If the employee has no manager, a `Maybe` with no value is returned.
96
+
1.`or` supplies a fallback in the case that the employee has no manager so that as long as an employee was originally found, all the following operations will execute.
97
+
1.`map` converts the manager to a new object which contains both the manager and employee.
98
+
1.`match` executes its `some` function if an employee was originally found and that employee has a manager. Since we supplied a fallback manager with `or`, the `some` function of `match` will execute if we found an employee. The `none` function of `match` executes if we didn't find any employees.
.pipe(log((f) =>`My fruit is ${f}`, 'information'))
165
-
.map((f) =>`${f} and banana`)
166
-
.pipe(log((f) =>`Now I have ${f}`));
167
-
```
100
+
See more examples of `Maybe`[in the docs](./docs/maybe.md) or [in the tests](./test/maybe).
168
101
169
102
### MaybeAsync
170
103
171
104
`MaybeAsync` represents a future value (`Promise`) that might or might not exist.
172
105
173
-
```typescript
174
-
function getFruit(day):Promise<string> {
175
-
returnPromise.resolve('apple');
176
-
}
106
+
`MaybeAsync` works just like `Maybe`, but since it is asynchronous, its methods accept a `Promise<T>` in most cases and all of its value accessing methods/getters return a `Promise<T>`.
177
107
178
-
const maybeAsync =MaybeAsync.from(getFruit());
179
-
180
-
const maybe =maybeAsync.toPromise();
181
-
182
-
console.log(maybe.getValueOrThrow()); // 'apple'
183
-
```
108
+
See more examples of `MaybeAsync`[in the docs](./docs/maybeAsync.md) or [in the tests](./test/maybeAsync).
184
109
185
110
### Result
186
111
187
-
`Result` represents a successful or failed operation.
112
+
`Result` represents a successful or failed operation. You can use it to declaratively define a process without needing to check if previous steps succeeded or failed. It can replace processes that use throwing errors and `try`/`catch` to control the flow of the application, or processes where errors and data are returned from every function.
188
113
189
114
```typescript
190
-
const successfulResult =Result.success('apple');
115
+
typeEmployee= {
116
+
id:number;
117
+
email:string;
118
+
firstName:string;
119
+
lastName:string;
120
+
managerId:number|undefined;
121
+
};
122
+
123
+
function getEmployee(employeeId):Employee|undefined {
sendReminder(email, `Remember to say hello to ${employeeFullName}`),
160
+
failure: (error) =>sendSupervisorAlert(error),
161
+
});
197
162
```
198
163
164
+
1.`try` executes the function to retrieve the employee, converting any thrown errors into a failed `Result` with the error message defined by the second parameter. If the employee is found, it returns a successful `Result`.
165
+
1.`ensure`'s callback is only called if an employee was successfully found. It checks if the employee works for the company by looking at their email address. If the address doesn't end in `@business.com`, a failed `Result` is returned with the error message defined in the second parameter. If the check passes, the original successful `Result` is returned.
166
+
1.`bind`'s callback is only called if the employee was found and works for the company. It converts the employee `Result` into another `Result`.
167
+
1.`toResult` converts a missing `managerId` into a failed `Result`. If there is a `managerId` value, it's converted into a successful `Result`.
168
+
1.`map`'s callback is only called if the `managerId` exists and converts the `managerId` into a new object to capture both the id and the employee's full name.
169
+
1.`bind`'s callback is only called if the original employee was found and that employee had a `managerId`. It converts the id and employee name into a new `Result`.
170
+
1.`try` now attempts to get the employee's manager and works the same as the first `try`.
171
+
1.`map`'s callback is only called if the original employee was found, has a `managerId` and that manager was also found. It converts the manager returned by `try` to a new object capturing both the manager and employee's name.
172
+
1.`match`'s `success` callback is only called if all the required information was retrieved and sends a reminder to the employee's manager. The `failure` callback is called if any of the required data could not be retrieved and sends an alert to the business supervisor with the error message.
173
+
174
+
See more examples of `Result`[in the docs](./docs/result.md) or [in the tests](./test/result).
175
+
199
176
### ResultAsync
200
177
201
178
`ResultAsync` represents a future result of an operation that either succeeds or fails.
202
179
180
+
`ResultAsync` works just like `Result`, but since it is asynchronous, its methods accept a `Promise<T>` in most cases and all of its value accessing methods/getters return a `Promise<T>`.
181
+
203
182
```typescript
204
183
function getLatestInventory():Promise<{ apples:number }> {
0 commit comments