-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2-stateless.js
More file actions
46 lines (37 loc) · 842 Bytes
/
2-stateless.js
File metadata and controls
46 lines (37 loc) · 842 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
42
43
44
45
46
'use strict';
const future = (executor) => ({
chain(fn) {
return future((resolve) => this.fork((value) => fn(value).fork(resolve)));
},
map(fn) {
return this.chain((value) => future.of(fn(value)));
},
fork(successed) {
executor(successed);
return this;
},
});
future.of = (value) => future((resolve) => resolve(value));
// Usage
const future1 = future((r) => r(5))
.map((x) => {
console.log('future1 started');
return x;
})
.map((x) => ++x)
.map((x) => x ** 3)
.fork((x) => {
console.log('future1 result', x);
});
console.dir({ future1 });
const promise1 = Promise.resolve(6)
.then((x) => {
console.log('promise1 started');
return x;
})
.then((x) => ++x)
.then((x) => x ** 3)
.then((x) => {
console.log('promise1 result', x);
});
console.dir({ promise1 });