SetImmediate is triggered to callbacka. Why?

Hello!

There is code:

var fs = require('fs');

fs.open(__filename, "r", function(err, file){
console.log("IO!");
});

setImmediate(function(){
console.log("immidiate");
});

process.nextTick(function(){
console.log("nextTick!");
});


The result:
nextTick!
immidiate
IO!


Although screencast https://learn.javascript.ru/screencast/nodejs#node... exactly the same code gives the following output:

nextTick!
IO!
immidiate


Why?

In advance I Express my deep gratitude to all who help!

UPD:

In documentation is written:
Schedules the "immediate" execution of callback after the I/O events' callbacks.
March 19th 20 at 08:42
1 answer
March 19th 20 at 08:44
process.nextTick runs to the nearest microtask (in this case will be on the current iteration of the event loop, after all the synchronous actions)
setImmediate - throws from the function to the end of the queue event-loop
fs.open - first will wait for the completion of the IO operation, and then will be in the queue of the event loop

theoretically, the IO operation can be completed before the call to setImmediate, then, colback fs.open will be called early, so got in the queue early
but most often sooner will setImmediate
That is, in the end of the current? Not in the beginning of the next? - Mose_Schowalt commented on March 19th 20 at 08:47
I still do not understand. In documentation is written:
setImmediate() fires on the following iteration or 'tick' of the event loop

Well and how it could work in front of IO, if triggered on the next iteration? - Mose_Schowalt commented on March 19th 20 at 08:50
Although on another page of docks it is written:
The function to call at the end of this turn of [the Node.js Event Loop]
- Mose_Schowalt commented on March 19th 20 at 08:53
@Mose_Schowalt, so kollbek will fall into place only when io get some work done. There is no guarantee that this will happen before the end of the current iteration. - Durward_Corkery commented on March 19th 20 at 08:56
@Durward_Corkerywhat you mean by IO? Timers, I / o? - Mose_Schowalt commented on March 19th 20 at 08:59
@Durward_Corkery, do Not quite understand, please explain. Why colback immediately to a queue that does not fall? - Mose_Schowalt commented on March 19th 20 at 09:02
@Mose_Schowalt, in this case, the file is opened. This is a system call - Durward_Corkery commented on March 19th 20 at 09:05

Find more questions by tags Node.js