async 包裹着的函数中进程是堵塞的 ,是同步化的, await等待的是个promise对象,否则"await" 对此表达式的类型没有影响
例1
async getDataDD(){await this.fun1()await this.fun2()// await Promise.all([this.fun1(),this.fun2()])//如果这样写则是并发进行的 会先打印2222 在打印1111console.log('444');this.fun3()},fun1(){ return new Promise((resolve)=>{setTimeout(()=>{console.log(1111);resolve('111')},6000)})},fun2(){ return new Promise((resolve)=>{setTimeout(()=>{console.log(2222);resolve('222')},6000)})},fun3(){console.log(3333)},
// 运行–this.getDataDD() 输出-- 1111 2222 ‘444’ 3333
例2
getDataDD2(){return new Promise((resolve=>{setTimeout(()=>{console.log(333)resolve('aa')},6000)}))},async test2(){console.log('111');var a = await this.getDataDD2()console.log('a--',a);console.log('222');},test1(){console.log('555');},
// 运行 this.test2() 输出— ‘111’ 333 'a–'aa ‘222’
// 运行了this.test2() 和this.test1() 则输出-- ‘111’ ‘555’ 333 'a–'aa ‘222’