宏任务和微任务,js队列执行顺序
微任务执行时机比宏任务早
宏任务: settimeout, setInterval, DOM事件, AJAX请求
微任务:Promise, async,await
console.log(1) setTimeout(() => { console.log(2) }) new Promise(function(resolve) { console.log('3') resolve() }).then(function() { console.log('4') }) console.log(5)
输出结果为 1,3,5,4,2
解析: 首先执行同步js,promise本身也是同步的,但它的then,catch却是异步的,执行完同步后,将执行微任务,也就是promise的then部分,最后才是宏任务settimeout