1 async function async1(){ 2 console.log('async1 start') //2 3 await async2() 4 //await async2()后面的内容可以看做是callback里得内容即是异步 5 //类似eventloop,settimeout 6 //Promise.resolve().then(()=>{console.log(async1 end)}) 7 console.log('async1 end') //5 8 } 9 10 async function async2(){ 11 console.log('async2') //3 12 } 13 14 console.log('script start') //1 15 async1() 16 console.log('script end') //4
变体
1 async function async1(){ 2 console.log('async1 start') //2 3 await async2() 4 //后面都是异步回调callback的内容 5 console.log('async1 end') //4 6 await async3() 7 console.log('async2 end') //6 8 } 9 10 async function async2(){ 11 console.log('async2') //3 12 } 13 14 async function async3(){ 15 console.log('async3') //5 16 } 17 18 console.log('script start') //1 19 async1() 20 console.log('script end') //4
异步函数 返回的是一个promise 内部await相当于then,await后面的是异步回调内容