-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_promise_implement.html
More file actions
51 lines (43 loc) · 1.29 KB
/
my_promise_implement.html
File metadata and controls
51 lines (43 loc) · 1.29 KB
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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<script>
const PENDING = new Symbol()
const FULFILLED = new Symbol()
const REJECTED = new Symbol()
class MyPromise {
// excutor 初始化 promise 时定义的回调函数
constructor(excutor) {
this.status = PENDING
// FULFILLED 状态下有 value
this.value = undefined
// REJECTED 状态下有 reason
this.reason = undefined
// 这两个函数是调动 then 方法的参数
this.onFulfilled = () => {
}
this.onRejected = () => { }
this.resolve = (value) => {
if (this.status === PENDING) {
this.status = FULFILLED
this.value = value
}
}
this.reject = (reason) => {
this.status = REJECTED
}
try {
excutor(resolve, reject)
} catch (error) {
}
}
then(onFulfilled, onRejected) {
return new MyPromise()
}
}
</script>
</html>