我的是 mac 环境 node 8.9.3, 代码与老师一致,但是打印的执行顺序中,属于第三阶段check的setImmediate 在 属于 第二阶段IO操作的readFile 之前执行。。。这就让我很困惑了。
const { readFile } = require('fs');
const EventEmitter = require('events');
const eve = new EventEmitter();
eve.on('hi', () => {
console.log('hi')
})
setTimeout(() => {
console.log('exec the 1st setTimeout timer')
}, 0)
setTimeout(() => {
console.log('exec the 2nd setTimeout timer')
}, 50)
setTimeout(() => {
console.log('exec the 3rd setTimeout timer')
}, 100)
readFile('./package.json', 'utf-8', data => {
console.log('exec the 1st read file')
})
readFile('./package.json', 'utf-8', data => {
console.log('exec the 2nd read file')
})
setImmediate(() => {
console.log('exec the 1st setImmediate')
})
Promise.resolve().then(() => {
process.nextTick(() => {
console.log('exec the 2nd nextTick')
})
eve.emit('hi')
console.log('exec the 1st promise')
})
.then(() => {
console.log('exec the 2nd promise')
})
process.nextTick(() => {
console.log('exec the 1st nextTick')
})