实现 Promise/A+ 规范 & ES6 Promise 方法

目录
  1. 1. 实现 Promise/A+ 规范
  2. 2. 实现 ES6 Promise 方法

实现 Promise/A+ 规范

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
186
187
188
189
190
191
192
// 三种状态
var status = {
pending: 'pending',
fulFilled: 'fulFilled',
rejected: 'rejected',
}

function Promise(exector) {
// 当前状态
this.status = status.pending;

// fulFilled状态的值
this.value = undefined;

// rejected状态的值
this.reason = null;

// fulFilled状态的回调函数
this.onFulfilledHandlers = [];

// rejected状态的回调函数
this.onRejectedHandlers = [];

const self = this;

// 状态从pending变为fulFilled
function resolve(value) {
if (value instanceof Promise) {
return value.then(resolve, reject);
}
if (self.status === status.pending) {
self.status = status.fulFilled;
self.value = value;
setTimeout(function () {
self.onFulfilledHandlers.forEach(function (h) {
h(self.value);
});
});
}
}

// 状态从pending变为rejected
function reject(reason) {
if (self.status === status.pending) {
self.status = status.rejected;
self.reason = reason;
setTimeout(function () {
self.onRejectedHandlers.forEach(function (h) {
h(self.reason);
});
});
}
}

// exector执行时可能会抛异常
// 如果不捕获,异常会抛到全局
try {
exector(resolve, reject);
} catch (e) {
reject(e);
}
}

// then方法
Promise.prototype.then = function (onFulfilled, onRejected) {
// 如果onFulfilled不是函数,则转为函数
if (typeof onFulfilled !== 'function') {
onFulfilled = function (value) {
return value;
}
}

// 如果onFulfilled不是函数,则转为函数
if (typeof onRejected !== 'function') {
onRejected = function (reason) {
throw reason;
}
}

const self = this;

// then返回一个promise
let promise2 = null;

// onFulfilled或onRejected的返回值
let x = undefined;

// 如果已经是fulFilled状态
if (this.status === status.fulFilled) {
promise2 = new Promise(function (resolve, reject) {
setTimeout(function () {
try {
x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
}

// 如果已经是rejected状态
if (this.status === status.rejected) {
promise2 = new Promise(function (resolve, reject) {
setTimeout(function () {
try {
x = onRejected(self.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
}

// 如果是pending状态
if (this.status === status.pending) {
promise2 = new Promise(function (resolve, reject) {
self.onFulfilledHandlers.push(function (value) {
try {
x = onFulfilled(value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
self.onRejectedHandlers.push(function (reason) {
try {
x = onRejected(reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
});
}

return promise2;
}

function resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
return reject(new TypeError('circular reference'));
}

let then = null;
let called = false;

if (x instanceof Promise) {
if (x.status === status.rejected) {
reject(x.reason);
} else {
x.then(function (y) {
resolvePromise(promise2, y, resolve, reject);
}, reject);
}
} else if (x != null && ((typeof x === 'object' || typeof x === 'function'))) {
try {
then = x.then;
if (typeof then === 'function') {
then.call(x, function (y) {
if (called) return;
called = true;
resolvePromise(promise2, y, resolve, reject);
}, function (r) {
if (called) return;
called = true;
reject(r);
});
} else {
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
} else {
resolve(x);
}
}

Promise.deferred = Promise.defer = function () {
var defer = {};
defer.promise = new Promise(function (resolve, reject) {
defer.resolve = resolve;
defer.reject = reject;
})
return defer;
}

module.exports = Promise;

检测通过

1
promises-aplus-tests Promise.js

实现 ES6 Promise 方法

1
2
3
Promise.prototype.catch = function (reject) {
this.then(null, reject);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Promise.all = function (promises) {
return new Promise(function (resove, reject) {
const result = [];
const len = promises.length;
const count = 0;
for (var i = 0; i < len; i++) {
promises[i].then(function (data) {
result[i] = data;
if (++count === len) {
resolve(result);
}
}, function (error) {
reject(error);
});
}
});
}
1
2
3
4
5
6
7
8
Promise.race = function (promises) {
return Promise(function (resolve, reject) {
var len = promises.length;
for (var i=0; i < len; i++) {
promises[i].then(resolve, reject);
}
});
}
1
2
3
4
5
6
Promise.resolve = function (value) {
if (value instanceof Promise) return value
return new Promise(function (resolve) {
resolve(value)
});
}
1
2
3
4
5
Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
return reject(value);
});
}