Skip to content

Instantly share code, notes, and snippets.

@canavci2016
Last active August 10, 2022 18:52
Show Gist options
  • Save canavci2016/b3386d9ba8be51b7b774c1bb169bbe50 to your computer and use it in GitHub Desktop.
Save canavci2016/b3386d9ba8be51b7b774c1bb169bbe50 to your computer and use it in GitHub Desktop.
event-loop-order-of-code
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');
});
});
@canavci2016
Copy link
Author

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