-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr.html
More file actions
92 lines (91 loc) · 3.28 KB
/
pr.html
File metadata and controls
92 lines (91 loc) · 3.28 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
<script>
class Promise {
constructor(excutorCallBack){
this.status = 'pending';
this.value = undefined;
this.fulfillAry = [];
this.rejectedAry = [];
let resolveFn = result => {
if(this.status !== 'pending') return;
this.status = 'fulfilled';
this.value = result;
let timer = setTimeout(() => {
this.fulfillAry.forEach(item=>item(this.value))
}, 0);
}
let rejectedFn = reason => {
if(this.value !== 'pedding') return;
this.status = 'rejected';
this.value = reason;
let timer = setTimeout(() => {
this.rejectedAry.forEach(item=>item(this.value))
}, 0);
}
try {
excutorCallBack(resolveFn,rejectedFn)
} catch(err) {
rejectedFn(err)
}
}
then(fulfilledCallBack,rejectedCallBack) {
typeof fulfilledCallBack !== 'function' ? fulfilledCallBack = result => result : null;
typeof rejectedCallBack !== 'function' ? rejectedCallBack = reason => {
throw new Error(reason instanceof Error ? reason.message : reason)
} : null;
return new Promise((reslove, reject) => {
if(this.status === 'fulfilled') {
let x = fulfilledCallBack(this.value);
x instanceof Promise ? x.then(reslove, reject):reslove(x)
}
if(this.status === 'rejected') {
let x = rejectedCallBack(this.value);
x instanceof Promise ? x.then(reslove, reject):reslove(x)
}
if(this.status === 'pending') {
this.fulfillAry.push(()=>{
try {
let x = fulfilledCallBack(this.value);
x instanceof Promise ? x.then(reslove, reject):reslove(x)
} catch(err) {
reject(err)
}
})
this.rejectedAry.push(()=>{
try {
let x = rejectedCallBack(this.value);
x instanceof Promise ? x.then(reslove,reject) : reslove(x);
} catch(err) {
reject(err);
}
})
}
})
}
catch(rejectedCallBack){
return this.then(null,rejectedCallBack);
}
static resolve (value) {
if (value instanceof Promise) return value
return new Promise(resolve => resolve(value))
}
}
var t1 = new Promise((resolve,rejected)=>{
resolve('test');
})
// t1.then((value)=>{console.log(value)})
setTimeout(()=>{
let f = t1.then((value)=>{console.log(value);return new Promise(function(r, j){
r('hehehe')
})}).then((value)=>{console.log(value)})
console.log(f)
},3000);
</script>
</html>