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’