await只能放在async的里面
只能接一个promise
<script>
function 摇(猜测){
return new Promise((resolve,reject)=>{
console.log('开始');
setTimeout(()=>{
let n = parseInt(Math.random()* 6 + 1,10);
if(n>3){
if(猜测 ==="大"){
resolve(n);
} else{
reject(n);
}
}else{
if(猜测 === "小"){
resolve(n);
} else {
reject(n);
}
}
},1000);
});
}
async function test() {
try{
let n = await 摇("大");
console.log("赢" + n);
}catch(error){
console.log("输" + error);
}
}
test();
</script>
//-------------------接收多个promise
/* async function test() {
try{
let n = await Promise.all([猜大小('大'),猜大小('小')]);
console.log("赢" + n);
}catch(error){
console.log("输" + error);
}
}
*/
网友评论