Take a look at the following code:
var fs = require('fs');
var pos = 0;
fs.stat(__filename, function() {
console.log(++pos + " FIRST STAT");
});
fs.stat(__filename, function() {
console.log(++pos + " LAST STAT");
});
setImmediate(function() {
console.log(++pos + " IMMEDIATE")
})
As I execute this code, the following result is displayed:
As Node.js documentation explains, the setImmediate is executed after I/O callbacks, but in this example setImmediate is being executed before I/O callbacks, am I missing something?
The result you are expecting would require the fs.stat methods to return instantaneously - basically before the current event loop ends. But your disk will need, let's say, 2ms to complete the I/O operation. Enough time to run through quite a few event loops. What happens (most likely now) is:
Loop 0 starts
fs.stat called
fs.stat called
immediate callback queued
Loop 0 ends
Loop 1 starts
immediate callback gets called
Loop 1 ends
Loop 2 starts
fs.stat completes, I/O callback queued
fs.stat completes, I/O callback queued
Loop 2 ends
Loop 3 starts
fs.stat I/O callback gets called
fs.stat I/O callback gets called
Actually no one even gurantees that fs.stat 1 completes before fs.stat2. So the result you have posted could also be
1 IMMEDIATE
2 LAST STAT
3 FIRST STAT
What you can do:
setImmediate
does NOT wait for all IO operations to finish. It merely gives priority to IO callbacks. At the time that you call setImmediate
here the fs.stat
calls simply aren't finished yet, and as such their callbacks aren't scheduled for the event loop yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With