概念:1、await 必须在async函数内、
2、await后面需要跟Promise对象才能达到想要的效果。
3、async函数本身就是一个promise
例子:
function dayin(){
return new Promise(function(resolve,reject){
setTimeout(()=>{
console.log("hello");
resolve();
},1000);
});
}
async function zhixing(){
await dayin().then(function(){//等待promise状态变化 这里是上面的resolve()执行后。
console.log("hehe");
});
return 'xxxx';
}
zhixing().then(function(res){//这里的res就是zhixing这个函数的返回值 xxxx
console.log(res);
});
//执行结果 分别是: hello hehe xxxx