Last active
October 21, 2018 07:49
-
-
Save shobhitchittora/a383dff32ef6db8d1cb712693bdbf5a2 to your computer and use it in GitHub Desktop.
Event Loop suedo-code
This file contains 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
// 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