-
Notifications
You must be signed in to change notification settings - Fork 2
/
p3.js
55 lines (54 loc) · 1.49 KB
/
p3.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
function Promise(executor){
let _=this;
_.value=undefined;
_.reason=undefined;
_.state="pending"//大家一开始都是一样,等着吧
_.resolveCallbacks=[];//callbacks在pending中添加,fullfilled中执行
_.rejectCallbacks=[];//callbacks在pending中添加,rejected中执行
function resolve(value){
_.value=value//实现之后的感言
_.state="fulfilled"//实现啦!
_.resolveCallbacks.forEach((fn)=>fn())
}
function reject(reason){
_.reason=reason//给我一个被拒绝的理由
_.state="rejected"//被拒绝了!
_.rejectCallbacks.forEach((fn)=>fn())
}
executor(resolve,reject)
}
Promise.prototype.then=function(onFulfilled, onRejected){
let _=this;
if(_.state=="pending"){
//把回调方法塞进队列中
_.resolveCallbacks.push(()=>{
onFulfilled(_.value)
})
_.rejectCallbacks.push(()=>{
onRejected(_.reason)
})
}
if(_.state=="fulfilled"){
onFulfilled(_.value)
}
if(_.state=="rejected"){
onRejected(_.reason)
}
}
//e.g
let Iagree=new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("我开心就同意了");
},1000)
})
//为了防止多次then,所以回调方法需要丢入队列中,防止方法被覆盖。
Iagree.then((data)=>{
console.log(Iagree.state)
},(e)=>{
console.log(e)
})
Iagree.then((data)=>{
console.log(Iagree.state+1)
},(e)=>{
console.log(e)
})