-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path9-debug.js
More file actions
51 lines (42 loc) · 963 Bytes
/
9-debug.js
File metadata and controls
51 lines (42 loc) · 963 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
47
48
49
50
51
'use strict';
let i = 0;
class Future {
#executor;
constructor(executor) {
this.id = i++;
console.log(`new Future ${this.id}`);
this.#executor = executor;
}
static of(value) {
return new Future((resolve) => resolve(value));
}
chain(fn) {
console.log(`chain ${this.id}`);
return new Future((resolve, reject) =>
this.fork(
(value) => {
console.log(`resolve ${this.id}`);
fn(value).fork(resolve, reject);
},
(error) => reject(error),
),
);
}
map(fn) {
console.log(`map ${this.id}`);
return this.chain((value) => {
console.log(`map.chain ${this.id}`);
return Future.of(fn(value));
});
}
fork(successed, failed) {
console.log(`fork ${this.id}`);
this.#executor(successed, failed);
}
}
// Usage
Future.of(5)
.map((x) => ++x)
.map((x) => x ** 3)
.map((x) => x * 2)
.fork((value) => console.log(`Result ${value}`));