forked from mauriciopoppe/implementing-promises-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
152 lines (144 loc) · 3.8 KB
/
promise.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// possible statuss
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class APromise {
constructor(fn) {
this.status = PENDING;
this.callbackQueue = [];
this.value = undefined;
try {
fn(this.resolve.bind(this), this.reject.bind(this));
} catch (error) {
this.reject(error);
}
}
resolve(value) {
this.changePromiseValue(this, FULFILLED, value);
}
reject(reason) {
this.changePromiseValue(this, REJECTED, reason);
}
changePromiseValue(p, status, value) {
if (p.status === PENDING) {
p.status = status;
p.value = value;
this.callbackQueue.forEach((cb) => {
const { onFulfilled, onRejected } = cb;
if (status === FULFILLED) {
onFulfilled(value);
} else {
onRejected(value);
}
});
}
}
then(onFulfilled, onRejected) {
function resolvePromise(p, value) {
if (p === value) {
return p.reject(
new TypeError("The promise and the return value are the same"),
);
} else if (typeof value === "object" || typeof value === "function") {
if (value === null) {
return p.resolve(value);
}
try {
var then = value.then;
} catch (error) {
return p.reject(error);
}
if (typeof then === "function") {
var called = false;
try {
then.call(
value,
function (y) {
if (called) return;
called = true;
resolvePromise(p, y);
},
function (r) {
if (called) return;
called = true;
p.reject(r);
},
);
} catch (error) {
if (called) return;
p.reject(error);
}
} else {
p.resolve(value);
}
} else {
p.resolve(value);
}
}
var that = this; // 保存一下this
let promise2 = new APromise(() => {});
if (this.status === FULFILLED) {
setTimeout(function () {
try {
if (typeof onFulfilled !== "function") {
promise2.resolve(that.value);
} else {
var x = onFulfilled(that.value);
resolvePromise(promise2, x);
}
} catch (error) {
promise2.reject(error);
}
}, 0);
}
if (this.status === REJECTED) {
setTimeout(function () {
try {
if (typeof onRejected !== "function") {
promise2.reject(that.value);
} else {
var x = onRejected(that.value);
resolvePromise(promise2, x);
}
} catch (error) {
promise2.reject(error);
}
}, 0);
}
// 如果还是PENDING状态,将回调保存下来
if (this.status === PENDING) {
that.callbackQueue.push({
onFulfilled: function () {
setTimeout(function () {
try {
if (typeof onFulfilled !== "function") {
promise2.resolve(that.value);
} else {
var x = onFulfilled(that.value);
resolvePromise(promise2, x);
}
} catch (error) {
promise2.reject(error);
}
}, 0);
},
onRejected: function () {
setTimeout(function () {
try {
if (typeof onRejected !== "function") {
promise2.reject(that.value);
} else {
var x = onRejected(that.value);
resolvePromise(promise2, x);
}
} catch (error) {
promise2.reject(error);
}
}, 0);
},
});
}
return promise2;
}
}
module.exports = APromise;