之前写过一个任务队列eventloop总结

https://www.wumao.org/782.html

里面没说到async await,今天这个小题坑到了


async function async1() {
    console.log('async1 start');
    await async2();
};

async function async2() {
    console.log('async2');
}

console.log('script start');

setTimeout(function() {
    console.log('setTimeout');
}, 0);

async1();

new Promise(function(resolve) {
    console.log('promise1');
    resolve();
}).then(function() {
    console.log('promise2');
});

console.log('script end2');

// script start,async1 start,async2, promise1,script end2,promise2, setTimeout

async await 实际上会加入到微任务队列,后面执行的代码相当于Promise.then操作的内容,会被加入到微任务队列中,而await后面接着的代码类似于new Promise(fn) 里面的fn,这时候这个回调是同步执行的。这里面标记下,以防下次遇到。

Last modification:December 13th, 2020 at 12:08 am
If you think my article is useful to you, please feel free to appreciate