@@ -29,11 +29,18 @@ It is a work in progress and the first monad implemented is the Either monad.
29
29
* [ Checking if an Option is Some or None] ( #checking-if-an-option-is-some-or-none )
30
30
* [ Try Monad] ( #try-monad )
31
31
* [ 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 )
37
44
<!-- TOC -->
38
45
39
46
## Installation
@@ -385,3 +392,73 @@ const failure = Try.execute(() => {
385
392
});
386
393
failure .isFailure (); // true
387
394
```
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