-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1-lazy.js
More file actions
55 lines (44 loc) · 1005 Bytes
/
1-lazy.js
File metadata and controls
55 lines (44 loc) · 1005 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
52
53
54
55
'use strict';
const id = (x) => x;
const future = (value) => {
let mapper = null;
const instance = {};
const map = (fn) => {
mapper = fn;
return future(instance);
};
const fork = (successed) => {
const finish = (product) => {
const transform = mapper ?? id;
const result = transform(product);
if (successed) successed(result);
};
if (value.fork) return void value.fork(finish);
finish(value);
};
return Object.assign(instance, { map, fork });
};
// Usage
const future1 = future(5)
.map((x) => {
console.log('future1 started');
return x;
})
.map((x) => ++x)
.map((x) => x ** 3)
.map((x) => {
console.log('future1 result', x);
});
console.dir({ future1 });
//future1.fork();
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 });