var fs = require('fs');
fs.readFile('1.txt', function(err, data) {
console.log('异步');
})
setTimeout(function() {
console.log('setTimeout');
}, 0);
process.nextTick(function() {
console.log('nextTick-a');
process.nextTick(function() {
console.log('nextTick-b');
})
})
setImmediate(function() {
console.log('setImmediate-a');
setImmediate(function() {
console.log('setImmediate-b');
})
})
/*
nextTick-a
nextTick-b
setTimeout
异步
setImmediate-a
setImmediate-b
*/
观察者优先级
在每次轮训检查中,各观察者的优先级分别是:
idle观察者 > I/O观察者 > check观察者。
idle观察者:process.nextTick
I/O观察者:一般性的I/O回调,如网络,文件,数据库I/O等
check观察者:setTimeout>setImmediate