-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (60 loc) · 1.8 KB
/
index.js
File metadata and controls
70 lines (60 loc) · 1.8 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function MyPromise(func) {
this.status = "pending";
this.value = undefined;
this.successCB = [];
this.errorCB = undefined;
this.finallyCB = undefined;
this.then = function (cb) {
this.successCB.push(cb);
return this;
}
this.catch = function (cb) {
this.errorCB = cb;
return this;
}
this.finally = function (cb) {
this.finallyCB = cb;
return this;
}
function waiter(index) {
if(index == this.successCB.length) {
if (this.finallyCB) this.finallyCB(this.value);
return;
}
var that = this;
if(this.value.constructor == MyPromise) {
this.value.then(x => {
that.value = that.successCB[index](x) || x;
waiter.call(that, ++index);
}).catch(x => {
if(that.errorCB) that.errorCB(x);
if (that.finallyCB) that.finallyCB(x);
return;
})
} else {
this.value = this.successCB[index](this.value) || this.value;
waiter.call(that, ++index);
}
}
function resolve(value) {
this.status = "success";
this.value = value;
waiter.call(this, 0);
}
function reject(value) {
this.status = "failure";
this.value = value;
this.errorCB(value);
if (this.finallyCB) this.finallyCB(this.value);
}
func(resolve.bind(this), reject.bind(this));
}
x = new MyPromise(function (resolve, reject) {
setTimeout(() => resolve(50), 5000);
})
y = function(val) {
return new MyPromise(function (resolve, reject) {
setTimeout(() => resolve(val*10), 1000);
})
}
x.then(v => 2 * v).then(v => y(v)).then(v => y(v)).then(console.log).finally(x => console.log('Yayy !', x));