-
Notifications
You must be signed in to change notification settings - Fork 300
Description
- Node.js Version: v10.14.1
- OS: Windows10x64
No need to init new project. Only the built-in fs module used there.
I have a code that works as designed in Unix based OS, but in windows, it gives the "strange" result.
Code
const fs = require('fs');
function someAsyncOperation(callback) {
fs.readFile('/path/to/file', callback);
}
const timeoutScheduled = Date.now();
setTimeout(() => {
const delay = Date.now() - timeoutScheduled;
console.log(`${delay}ms have passed since I was scheduled`);
}, 1000);
someAsyncOperation(() => {
console.log('BEFORE WHILE');
const startCallback = Date.now();
while (Date.now() - startCallback < 1000) {
// do nothing
}
// setImmediate(() => { console.log('IMMEDIATE'); });
console.log('AFTER WHILE');
});The output is
BEFORE WHILE
AFTER WHILE
2007ms have passed since I was scheduledThe output is the same independently of how many timers are present. ( it's always 2000+ )
Which is weird. I guess that such a result is possible if the setTimeout ( I mean the setTimeout itself, not thesetTimeout's callback ) registered AFTER while loop work.
Pay attention at // setImmediate(() => { console.log('IMMEDIATE'); }); line.
If I uncomment it the output will be
BEFORE WHILE
AFTER WHILE
IMMEDIATE
1005ms have passed since I was scheduledIn Unix based system the output always {1000+}ms, and doesn't depend on setImmediate existence.
Can someone explain why it happens, and give event loop execution order.
It's maybe a BUG, but I'm not sure.
Thanks in advance.