看看这些代码行
function someAsyncOperation () {
console.log("inside someAsyncOperation");
var startCallback = Date.now();
// do something that will take 10ms...
while (Date.now() - startCallback <= 100) {
; // do nothing
}
}
someAsyncOperation();
var timeoutScheduled = Date.now();
setImmediate(function () {
var delay = Date.now() - timeoutScheduled;
console.log(delay + "ms have passed since I was scheduled");
});
someAsyncOperation();
fs.readFile("./noor.txt",function(err,data){
var delay = Date.now() - timeoutScheduled;
console.log(delay + "file read");
});
据我所知,setImmediate回调在任何 I/O 操作后的事件循环结束时运行。但在我的情况下,在文件读取操作返回之前调用 setImmediate 回调。请解释为什么会发生这种情况或 setImmediate 回调执行背后的逻辑是什么。