Last active
August 10, 2022 18:52
-
-
Save canavci2016/b3386d9ba8be51b7b774c1bb169bbe50 to your computer and use it in GitHub Desktop.
event-loop-order-of-code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(() => { console.log('timeout'); }, 0); | |
setImmediate(() => { console.log('immediate'); }); | |
const fs = require('fs'); | |
fs.readFile(__filename, () => { | |
setTimeout(() => { | |
console.log('timeout'); | |
}, 0); | |
setImmediate(() => { | |
console.log('immediate'); | |
}); | |
}); |
The main advantage to using setImmediate() over setTimeout() is setImmediate() will always be executed before any timers if scheduled within an I/O cycle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you run the test.js which the two firsts scripts arent within the I/O cycle. the order in which the two timers are executed is non-deterministic