I find it easier to wrap my head following the execution path in more detail, especially the path that logMsg accepts. A good debugger is great for this.
Nothing happens until in a loop where a variable called message is defined. At the beginning, it will be the Ferrari Saw.
There is also an anonymous function next to the โmessageโ, which, unfortunately, is difficult to separate and define. Since it goes beyond its scope for a variable called "message" that falls inside this loop, we could not do on line 6:
var someFunction = function(){ console.log("Normal Callback: " + message); }
... because what kind of "message"? Nothing outside this for-loop has direct access to the โmessageโ (other than closing, but don't worry about that yet). Please note that this function will not be executed yet. It has just been created at this moment.
So the next one is done by LogCar("Saw a Ferrari", someFunction...) . Where is LogCar? Oh top. Let it jump there, but for simplicity let it skip process.nextTick. Essentially someFunction("Saw a Ferrari") happens here. Where is some function? Oh, this anonymous function that has not yet been executed. It's time to execute it. Thus, the process jumps back to see what is inside it and execute it, which is console.log("Saw a Ferrari");
This is done, and the process is repeated for "Saw a Porsche".
source share