forked from georgewing/promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
185 lines (176 loc) · 5.83 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
const PENDING = 'pending';
const resolvePromise = (promise2, x, resolve, reject) => {
let isCalled = false
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected for promise #<Promise>'));
}
// 为了能兼容其他符合 PromiseA+标准的promise 如: bluebird q es6-promise
if (x instanceof Promise) { // 对应标准2.3.2
if (x.status === PENDING) {
x.then(value => {
resolvePromise(promise2, value, resolve, reject);
}, reject)
} else {
x.then(resolve, reject)
}
return
}
if (((typeof x === 'obejct') && (x !== null)) || (typeof x === 'function')) {
try {
let then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
if (isCalled) return;
isCalled = true;
// 实现递归解析
resolvePromise(promise2, y, resolve, reject);
},e => {
if (isCalled) return;
isCalled = true;
reject(e);
});
} else {
resolve(x);
}
} catch(ex) {
if (isCalled) return;
isCalled = true;
reject(ex);
}
} else {
resolve(x);
}
}
class Promise {
constructor(executor) {
this.status = PENDING;
this.value = undefined;
this.reason = undefined;
// 发布订阅模式
this.doneCallbacks = []
this.failCallbacks = []
let resolve = (value) => {
if (value instanceof Promise) {
return value.then(resolve, reject); // 递归解析resolve中的参数 直到不是promise对象
}
if (this.status === PENDING) {
this.value = value;
this.status = FULFILLED;
this.doneCallbacks.forEach(fn => fn())
}
}
let reject = (reason) => {
if (this.status === PENDING) {
this.reason = reason;
this.status = REJECTED;
this.failCallbacks.forEach(fn => fn())
}
}
try {
executor(resolve, reject);
} catch(ex) {
reject(ex);
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : val=>val
onRejected = typeof onRejected === 'function' ? onRejected: err=>{ throw err }
let promise2 = new Promise((resolve, reject) => {
if (this.status === FULFILLED) {
let x = onFulfilled(this.value);
setTimeout(() => {
try {
resolvePromise(promise2, x, resolve, reject);
} catch (ex) {
reject(ex);
}
}, 0);
}
if (this.status === REJECTED) {
let x = onRejected(this.reason);
setTimeout(() => {
try {
resolvePromise(promise2, x, resolve, reject);
} catch (ex) {
reject(ex);
}
}, 0)
}
if (this.status === PENDING) {
this.doneCallbacks.push(() => { // 订阅
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (ex) {
reject(ex);
}
}, 0);
})
this.failCallbacks.push(() => {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch(ex) {
reject(ex);
}
}, 0);
})
}
})
return promise2;
}
catch(failCallback) {
return this.then(null, failCallback);
}
static resolve(data) {
return new Promise((resolve, reject) => {
resolve(data)
})
}
static reject(data) {
return new Promise((resolve, reject) => {
reject(data)
})
}
//finally 是无论如何都会执行的意思
//如果返回一个promise,会等待这个promise也执行完毕
finally(callback) {
return this.then(
x => Promise.resolve(callback()).then(() => x),
e => Promise.reject(callback()).then(() => {throw e})
)
}
all(promises) {
let resolvedCount = 0;
let promisesLen = promises.length;
let results = new Array(promisesLen);
return new Promise((resolve, reject) => {
for (let i = 0; i < promisesLen; i++) {
(function(i) {
Promise.resolve(val).then(value => {
resolvedCount++;
results[i] = value;
// 当所有函数都正确执行了,resolve输出所有返回结果
if (resolvedCount === promisesLen) {
return resolve(results);
}
}, reason => reject(reason))
})(i)
}
resolve(results);
})
}
}
Promise.defer = Promise.deferred = function() {
let dfd = {};
dfd.promise = new Promise((resolve, reject) => {
dfd.resolve = resolve;
dfd.reject = reject;
})
return dfd;
}
module.exports = Promise