Skip to content

Instantly share code, notes, and snippets.

@OlivierAlbertini
Created June 22, 2018 23:00
Show Gist options
  • Save OlivierAlbertini/dac383bbdc9b36dc2836338cc6d9ced9 to your computer and use it in GitHub Desktop.
Save OlivierAlbertini/dac383bbdc9b36dc2836338cc6d9ced9 to your computer and use it in GitHub Desktop.
const pendingTimers = [];
const pendingOSTasks = [];
const pendingOperations = [];
// New timers, tasks, operations are recorded from myFile running
myFile.runContents();
function shouldContinue() {
// Check one: Any pending setTimeout, setInterval, setImmediate?
// Check two: Any pending OS tasks? (Like server listening to port)
// Check three: Any pending long running operations? (Like fs module)
return (
pendingTimers.length || pendingOSTasks.length || pendingOperations.length
);
}
// Entire body executes in one 'tick'
while (shouldContinue()) {
// 1) Node looks at pendingTimers and sees if any functions
// are ready to be called. setTimeout, setInterval
// 2) Node looks at pendingOSTasks and pendingOperations
// and calls relevant callbacks
// 3) Pause execution. Continue when...
// - a new pendingOSTask is done
// - a new pendingOperation is done
// - a timer is about to complete
// 4) Look at pendingTimers. Call any setImmediate
// 5) Handle any 'close' events
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment