首先列举一下宏观任务和微观任务、
宏观任务
1.js逻辑代码、
2.setTImeout、setInterval
3.Ui render(渲染页面)
4.io(例如读写文件)
微观任务
1.Promise
2.async await
3.Process.nextTick
4.宏观任务中逻辑代码中的代码块(除了含有宏观任务的代码块)
执行顺序:
1.先执行同步任务,再执行宏观任务中微观任务队列,(有一个点在于promise 中.then .catch 是同步任务 会在第一时间执行)
2.微观任务队列执行完毕后再执行微观任务队列中的宏观任务,
3.如果微观任务队列中有宏观任务,
4.则在执行完当前微观任务队列 之后再执行这个宏观任务,*
打印序列:
console.log('script start') 1
setTimeout(function() {
console.log('setTimeout') 6
}, 0)
new Promise(resolve => {
console.log('Promise') 2
resolve()
}).then(function() {
console.log('promise1') 4
}).then(function() {
console.log('promise2') 5
})
console.log('script end') 3