Answer the question
In order to leave comments, you need to log in
SetImmediate fires before the callback. Why?
Hello!
There is a 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!");
});
nextTick!
immidiate
IO!
nextTick!
IO!
immidiate
Schedules the "immediate" execution of the callback after I/O events' callbacks.
Answer the question
In order to leave comments, you need to log in
process.nextTick - executed in the next microtask (in this case, it will be at the current iteration of the event-loop, immediately after all synchronous actions)
setImmediate - immediately throws the function at the end of the event-loop
queue fs.open - first waits for the IO operation to complete, and only then will be in the event-loop queue
theoretically IO operation can complete before setImmediate is called, then the fs.open callback will be called earlier, since it got into the queue earlier
, but most often it will still be setImmediate earlier
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question