forked from xzlaptt/React
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.html
144 lines (114 loc) · 2.77 KB
/
promise.html
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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Promise练习</title>
</head>
<body>
</body>
<script>
//创建并定义一个新的Promise对象
const p = new Promise((resolve,reject)=>{
//执行异步操作
setTimeout(()=>{
//如果当前事件是偶数就代表成功,否则就代表失败
const time = Date.now();
console.log(time)
if(time %2 === 0){
//成功的调用
resolve("成功了");
}else{
//失败了调用
reject("失败了");
}
},1000);
});
setTimeout(()=>{
p.then(
value =>{ //接收得到成功的value的数据
console.log(value);
},
reason =>{//接收得到失败的reason的数据
console.log(reason);
}
)
},2000)
setHui=()=>{
console.log("这是set")
}
setHui(2);
setHui =(a,getHui,setHui)=>{
setTimeout(()=>{
if(a === 0){
getHui();
}else(
setHui()
)
},1000)
};
async function show(){
return "sss"
};
const s = show();
console.log(s);
const newSet = new Promise((resolve,reject)=>{
setTimeout(()=>{
console.log("成功");
resolve();
},1000)
}).then(
value =>{
throw 5;
}
).catch((reason) =>{
console.log("catch"+reason)
})
console.log(newSet," zheg ");
//Promise.all([p,p1,p2])
// const p1 = new Promise((resolve,reject)=>{
// resolve();
// })
// const p2 = new Promise((resolve,reject)=>{
// reject();
// })
// Promise.reject()
f1 = () =>{
return new Promise((resolve,reject)=>{
// resolve(1);
reject("错误")
})
}
async function test(){
try{
const p = await f1();
console.log(p)
}catch(error){
console.error(error)
}
}
test();
async function test2(){
return 1;
}
console.log(test2());
async function test3(){
return new Promise((resolve,reject)=>{
resolve("test3")
});
}
test3().then(
value =>{
console.log("这个是成功的value");
},
reason =>{
console.log("reason")
}
)
history.back();
history.forward();
history.listen(()=>{
console.log("监听路径变化")
});
history.replaceState
</script>
</html>