Skip to content

Commit b4488e3

Browse files
committed
增加promise实现
1 parent ae1b73f commit b4488e3

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

promise.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>promise</title>
6+
<script src="promise.js"></script>
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>

promise.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* promise实现
3+
*/
4+
5+
const isFunction = (val) => {
6+
return typeof val === 'function';
7+
};
8+
9+
function Promise (Fn) {
10+
// 初始为'PENDING'状态
11+
this.status = 'PENDING';
12+
this.value;
13+
this.reason;
14+
// 存储onFulfilled
15+
this.resolves = [];
16+
// 存储onRejected
17+
this.rejects = [];
18+
// let resolve = (value) => {
19+
// if (this.status === 'PENDING') {
20+
// // 加上异步处理,保证then方法先执行
21+
// setTimeout(() => {
22+
// //状态转换为FULFILLED
23+
// //执行then时保存到resolves里的回调
24+
// //如果回调有返回值,更新当前value
25+
// this.status = 'FULFILLED';
26+
// this.resolves.forEach(fn => {
27+
// value = fn(value) || value;
28+
// });
29+
// this.value = value;
30+
// });
31+
// }
32+
// }
33+
// let reject = (reason) => {
34+
// if (this.status === 'PENDING') {
35+
// // 加上异步处理,保证reject方法先执行
36+
// setTimeout(() => {
37+
// //状态转换为REJECTED
38+
// //执行then时保存到rejects里的回调
39+
// //如果回调有返回值,更新当前reason
40+
// this.status = 'REJECTED';
41+
// this.rejects.forEach(fn => {
42+
// reason = fn(reason) || reason;
43+
// });
44+
// this.reason = reason;
45+
// });
46+
// }
47+
// }
48+
let transition = (status, val) => {
49+
if (this.status === 'PENDING') {
50+
// 加上异步处理,保证方法先执行
51+
setTimeout(() => {
52+
//状态转换为REJECTED
53+
//执行then时保存到rejects里的回调
54+
//如果回调有返回值,更新当前reason
55+
this.status = status;
56+
let f = status === 'FULFILLED',
57+
queue = this[f ? 'resolves' : 'rejects'];
58+
queue.forEach(fn => {
59+
val = fn(val) || val;
60+
});
61+
this[f ? 'value' : 'reason'] = val;
62+
});
63+
}
64+
}
65+
let resolve = (val) => {
66+
transition('FULFILLED', val);
67+
}
68+
let reject = (reason) => {
69+
transition('REJECTED', reason);
70+
}
71+
try {
72+
Fn(resolve, reject);
73+
} catch (err) {
74+
reject(err);
75+
}
76+
}
77+
78+
Promise.prototype.then = (onFulfilled, onRejected) => {
79+
if (this.status === 'PENDING') {
80+
isFunction(onFulfilled) && this.resolves.push(onFulfilled);
81+
isFunction(onRejected) && this.rejects.push(onRejected);
82+
} else if (this.status === 'FULFILLED'){
83+
isFunction(onFulfilled) && onFulfilled(this.value);
84+
} else if (this.status === 'REJECTED'){
85+
isFunction(onRejected) && onRejected(this.reason);
86+
}
87+
return this;
88+
}
89+
Promise.prototype.catch = (onRejected) => {
90+
return this.then(null, onRejected);
91+
}
92+
let getInfo = new Promise((resolve, reject) => {
93+
resolve('success');
94+
}).then(r => {
95+
console.log(r);
96+
return r + '1';
97+
}).then(r => {
98+
console.log(r);
99+
return r + '2';
100+
});
101+
102+
setTimeout(() => {
103+
getInfo.then(r => {
104+
console.log(r);
105+
return r + '3';
106+
});
107+
});

0 commit comments

Comments
 (0)