// loop.js
// node index.js
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 calls relevant callbacks.
// 3. Node looks at pendingOperations and calls relevant callbacks.
// 4. Pause execution. Continue when...
// - a timer is about to complete.
// - a new pendingOSTask is done.
// - a new pendingOperation is done.
// 5. Look at pendingTimers. Call any setImmediate.
// 6. Handle any `close` events.mn
}
// exit back to terminal