Skip to content

Commit e583da2

Browse files
committed
docs(monad): add future monad documentation
1 parent edf2033 commit e583da2

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

README.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ It is a work in progress and the first monad implemented is the Either monad.
2929
* [Checking if an Option is Some or None](#checking-if-an-option-is-some-or-none)
3030
* [Try Monad](#try-monad)
3131
* [Usage](#usage-2)
32-
* [Using `map`](#using-map-1)
33-
* [Using `flatMap`](#using-flatmap-1)
34-
* [Matching a Try](#matching-a-try)
35-
* [Handling errors in Infrastructure code](#handling-errors-in-infrastructure-code)
36-
* [Checking if a Try is Success or Failure](#checking-if-a-try-is-success-or-failure)
32+
* [Using `map`](#using-map-1)
33+
* [Using `flatMap`](#using-flatmap-1)
34+
* [Matching a Try](#matching-a-try)
35+
* [Handling errors in Infrastructure code](#handling-errors-in-infrastructure-code)
36+
* [Checking if a Try is Success or Failure](#checking-if-a-try-is-success-or-failure)
37+
* [Future Monad](#future-monad)
38+
* [Usage](#usage-3)
39+
* [Creating a Future](#creating-a-future)
40+
* [Mapping over a Future](#mapping-over-a-future)
41+
* [Using `flatMap`](#using-flatmap-2)
42+
* [Using `map`](#using-map-2)
43+
* [Evaluate a Future](#evaluate-a-future)
3744
<!-- TOC -->
3845

3946
## Installation
@@ -385,3 +392,73 @@ const failure = Try.execute(() => {
385392
});
386393
failure.isFailure(); // true
387394
```
395+
396+
## Future Monad
397+
398+
The `Future` monad represents a computation that may be executed asynchronously.
399+
400+
### Usage
401+
402+
#### Creating a Future
403+
404+
You can create a `Future` using the static method `Future.of`.
405+
406+
```typescript
407+
import { Future } from '@leanmind/monads';
408+
409+
const future = Future.of(() => Promise.resolve(42));
410+
```
411+
412+
#### Mapping over a Future
413+
414+
You can use the `map` or `flatMap` method to transform the computed value inside a `Future`. The operation will not
415+
execute the transformation (_lazy evaluation_) until `complete` method is called.
416+
417+
##### Using `flatMap`
418+
419+
```typescript
420+
import { Future } from '@leanmind/monads';
421+
422+
const future = Future.of(() => Promise.resolve(42))
423+
.flatMap(x => Future.of(() => Promise.resolve(x + 1)))
424+
.complete(
425+
x => console.log(x),
426+
err => console.error(err)
427+
); // 43
428+
```
429+
430+
##### Using `map`
431+
432+
```typescript
433+
import { Future } from '@leanmind/monads';
434+
435+
const future = Future.of(() => Promise.resolve(42))
436+
.map(x => x + 1)
437+
.complete(
438+
x => console.log(x),
439+
err => console.error(err)
440+
); // 43
441+
```
442+
443+
#### Evaluate a Future
444+
445+
You can evaluate a `Future` using the `complete` method. The `complete` method takes two functions as arguments:
446+
one for the success case and one for the failure case.
447+
448+
```typescript
449+
import { Future } from '@leanmind/monads';
450+
451+
const successFuture = Future.of(() => Promise.resolve(42));
452+
453+
await successFuture.complete(
454+
x => console.log(x),
455+
err => console.error(err)
456+
); // 42
457+
458+
const failureFuture = Future.of(() => Promise.reject(new Error('Error')));
459+
460+
await failureFuture.complete(
461+
x => console.log(x),
462+
err => console.error(err)
463+
); // Error('Error')
464+
```

0 commit comments

Comments
 (0)