• async/await是小妹异步回调的终极武器
    • JS是单线程,还是有异步,还得基于event loop
    • async/await只是一个语法糖
    1. async function async1() {
    2. console.log('async1 start')
    3. await async2()
    4. // await 后面的行代码,都可以看作是 callback 里的内容,即异步
    5. // 类似 event loop,setTimeout(cb1)
    6. // setTimeout(function () { console.log('async1 end') })
    7. // Promise.resolve().then(() => { console.log('async1 end') })
    8. console.log('async1 end')
    9. }
    10. async function async2() {
    11. console.log('async2')
    12. }
    13. console.log('script start')
    14. async1()
    15. console.log('script end')
    16. // script start
    17. // async1 start
    18. // async2
    19. // async1 end
    20. // script end