-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
214 lines (199 loc) · 6.55 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Promise 的三种状态
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
function MyPromise(executor) {
let _this = this; // 拷贝this指针
_this.state = PENDING; // 初始状态
_this.value = undefined; // 成功结果
_this.reason = undefined; // 失败原因
_this.onFulFilledCallbacks = [];
_this.onRejectedCallbacks = [];
// 如果executor是同步代码 进行try catch获取其中的异常 如果有异常 把异常传到reject
try {
executor(resolve, reject); // 立即执行
} catch (e) {
reject(e); // 调用reject并把捕获的error作为参数传给reject
}
function resolve(value) {
// 当状态为pending时再做更新
if (_this.state === PENDING) {
_this.value = value; // 保存成功结果
_this.state = FULFILLED;
_this.onFulFilledCallbacks.forEach((fn) => {
fn();
});
}
}
function reject(reason) {
// 当状态为pending时再做更新
if (_this.state === PENDING) {
_this.reason = reason; // 保存失败原因
_this.state = REJECTED;
_this.onRejectedCallbacks.forEach((fn) => {
fn();
});
}
}
}
MyPromise.prototype.then = function (onFulfilled, onRejected) {
let _this = this;
// 成功和失败默认不传, 给一个默认函数 可以实现值的穿透
onFulfilled = typeof onFulfilled === 'function'? onFulfilled:function(value) {
return value;
}
// 在值的穿透的情况下 应该走下一个then的onRejected而不是onFulfiled 保证逻辑的一致性
onRejected = typeof onRejected === 'function'? onRejected:function(err) {
throw err;
}
let promise2 = new MyPromise((resolve,reject) => {
if(_this.state === FULFILLED){
setTimeout(() => { // 用setTimeOut实现异步
try{
let x = onFulfilled(_this.value); // x可能是普通值 也可能是一个promise, 还可能是别人的promise
resolvePromise(promise2,x,resolve,reject); // 写一个方法统一处理
}catch(e){
reject(e);
}
},0);
}
if(_this.state === REJECTED) {
setTimeout(() => {
try{
let x = onRejected(_this.reason);
resolvePromise(promise2,x,resolve,reject);
}catch(e){
reject(e);
}
},0);
}
if(_this.state === PENDING) {
_this.onFulFilledCallbacks.push(function() {
setTimeout(() => {
try{
let x = onFulfilled(_this.value);
resolvePromise(promise2,x,resolve,reject);
}catch(e){
reject(e);
}
},0);
});
_this.onRejectedCallbacks.push(function(){
setTimeout(() => {
try{
let x = onRejected(_this.reason);
resolvePromise(promise2,x,resolve,reject);
}catch(e){
reject(e);
}
},0);
});
}
});
return promise2;
};
/**
* 解析then返回值与新Promise对象
* @param {Object} promise2 新的Promise对象
* @param {*} x 上一个then的返回值
* @param {Function} resolve promise2的resolve
* @param {Function} reject promise2的reject
*/
function resolvePromise(promise2, x, resolve, reject) {
if(promise2 == x) {
return reject(new TypeError('循环引用了!'));
}
// 看x是不是一个promise promise应该是一个对象
let called; // 表示是否调用过成功或者失败
if (x!== null && (typeof x ==='object' ||typeof x === 'function')) {
// 可能是promise 看这个对象中是否有then 如果有 姑且作为promise 用try catch防止报错
try {
let then = x.then;
if (typeof then === 'function') {
// 成功
then.call(x, function(y) {
if (called) return; // 避免别人写的promise中既走resolve又走reject的情况
called = true;
resolvePromise(promise2, y, resolve, reject)
}, function(err) {
if (called) return
called = true;
reject(err);
})
} else {
resolve(x); // 如果then不是函数 则把x作为返回值.
}
} catch (e) {
if (called) return
called = true;
reject(e);
}
} else { // 普通值
return resolve(x);
}
}
MyPromise.prototype.catch = function(errFn) {
return this.then(null,errFn);
}
MyPromise.prototype.finally = function(fn) {
this.then(()=>{
fn();
},()=>{
fn();
});
return this;
}
MyPromise.all = function(values) {
return new MyPromise((resolve,reject) => {
let results = [];
let index = 0;
function addToArr(key,value) {
index++;
results[key] = value;
if(index === values.length) {
resolve(results);
}
}
for(let i = 0; i < values.length; i++) {
let current = values[i];
if(current && current.then && typeof current.then === 'function') {
current.then((value) => {
addToArr(i,value);
},reject);
}else{
addToArr(i,current);
}
}
});
}
MyPromise.race = function(values) {
return new MyPromise((resolve,reject) => {
for(let i = 0; i < values.length; i++) {
let current = values[i];
if(current && current.then && typeof current.then === 'function') {
current.then(resolve,reject);
}else{
resolve(current);
}
}
});
}
MyPromise.resolve = function(value) {
return new MyPromise((resolve,reject) => {
resolve(value);
});
}
MyPromise.reject = function(reason) {
return new MyPromise((resolve,reject) => {
reject(reason);
});
}
MyPromise.deferred = function() {
let dfd = {};
dfd.promise = new MyPromise((resolve, reject) => {
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
}
module.exports = MyPromise;