- 宏任务:
script
、setTimeOut
、setInterval
、setImmediate
- 微任务:
promise.then
,process.nextTick
、Object.observe
、MutationObserver
- 注意:Promise是同步任务
先执行同步任务 在执行微任务,在执行宏任务
console.log('1');
setTimeout(function() {
console.log('2');
new Promise(function(resolve) {
console.log('3');
resolve()
}).then(function() {
console.log('4');
})
})
new Promise(function(resolve) {
console.log('5');
resolve()
}).then(function() {
console.log('6');
})
setTimeout(function() {
console.log('7');
new Promise(function(resolve) {
console.log('8');
resolve()
}).then(function() {
console.log('9');
})
})
//执行结果 1 5 6 2 3 4 7 8 9