-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiramius.js
51 lines (40 loc) · 1.08 KB
/
tiramius.js
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
const INTERNAL = function (){};
function Tiramius(executor) {
this._fullfillHandler = undefined;
if (typeof executor === 'function') {
executor(this.resolve.bind(this));
}
}
Tiramius.prototype.resolve = function (obj) {
let result = null;
if (this._fullfillHandler) {
result = this._fullfillHandler(obj);
}
if (this._chain) {
if (result instanceof Tiramius) {
result._fullfillHandler = this._chain._fullfillHandler;
return result;
}
this._chain.resolve(result);
}
return new Tiramius(INTERNAL);
}
Tiramius.prototype.then = function (didFullfill) {
this._fullfillHandler = didFullfill;
const tiramius = this._chain = new Tiramius(INTERNAL);
return tiramius;
}
/****** Test Code ******/
function delay(time) {
return new Tiramius((resolve) => {
setTimeout(() => {
console.log('timeout!!', time);
resolve();
}, time);
})
}
console.log('start');
delay(1000)
.then(() => { console.log('a'); return 'hahah'; })
.then((data) => { console.log('b', data); return delay(3000); })
.then(() => { console.log('c'); });