Skip to content

Instantly share code, notes, and snippets.

@shobhitchittora
Last active October 21, 2018 07:49
Show Gist options
  • Save shobhitchittora/a383dff32ef6db8d1cb712693bdbf5a2 to your computer and use it in GitHub Desktop.
Save shobhitchittora/a383dff32ef6db8d1cb712693bdbf5a2 to your computer and use it in GitHub Desktop.
Event Loop suedo-code
// my JS code
// node index.js
index.runCode();
// Node JS event loop
var pendingTimers = [];
var pendingOSTasks = [];
var pendingOperations = [];
function runNextTick(){
/**
1) pending setTimeout, setInterval, setImmediate
2) pending OS tasks ( http server listen )
3) pending long running tasks ( fs read )
*/
return pendingTimers.length || pendingOSTasks.length || pendingOperations.length;
}
while(runNextTick()){
// New `tick`
/**
1) Check for pending timers ( setTimeout and setInterval )
2) Check for OS tasks and long operations
3) Pause execution. Continue only when -
- pending timer to complete
- pending os tasks is done
- pending operation is done
4) Look for pending timers ( setImmediate )
5) Handle any `close` events
*/
}
// exists back to terminal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment