-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path9-maybe-catch.js
More file actions
41 lines (34 loc) · 669 Bytes
/
9-maybe-catch.js
File metadata and controls
41 lines (34 loc) · 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
const doMonad = require('./helpers/do-notation.js');
const Nothing = {
then() {
return Nothing;
},
catch(callback) {
callback();
return this;
},
};
class Just {
constructor(value) {
this.value = value;
}
then(fn) {
const result = fn(this.value);
if (result instanceof Just || result === Nothing) {
return result;
}
return new Just(result);
}
catch() {
return this;
}
}
doMonad(function* () {
const a = yield new Just(5);
const b = yield new Just(3);
const c = yield new Just(4);
console.log((a + b) * c);
}).catch(() => {
console.log('At least one value is not present');
});