Have look on these lines of code
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");
});
As I came to know that setImmediate callback is run at the end of the event loop after any I/O operation. But in my case the the setImmediate callback is called before the file read operation returns. Kindly explain why is this happening or what's the logic behind setImmediate callback execution.