2/19/2023 0 Comments Nodejs event loop![]() ![]() If you had a Javascript implementation with no event loop (which is possible), then you would still have a call stack to manage the calling of functions and the returning from those function calls. It's a mechanism for managing function calls in the core Javascript interpreter (as described above). The call stack is not part of the event loop. I did not see any phase that are particularly designed to handle the function in call stack If something is found, the callback corresponding to that event is then called and so on. When that timer callback eventually returns back from executing, then control again goes back to the event loop and it resumes checking its various phases for any pending work to be executed. If that callback function itself called any other functions, then the call stack would then be used to keep track of function return locations. That callback would start running a sequence of Javascript code. At that point, the interpreter would take the callback associated with the timer event and call that callback. The time that the timer was set for has been met or surpassed. Let's assume there's a timer waiting to run. The event loop will go through it's various phases, looking for work waiting to be executed. If there are none of those waiting in the PromiseJobQueue, then control to the event loop. When that last console.log("all done") statement is executed, the call stack is empty and the current function is done (remember all nodejs modules are actually in a function) and then interpreter has no more code to run.Īt that point, the interpreter will first look for special things to run such as PromiseJobs (resolved promises waiting to call their. When a() gets to it's last line of code and executes return, the interpreter looks for the most recent return address on the stack and jumps to that point, which will be the console.log() statement right after where a() was called.Īt this point, the call stack will be empty. When b() gets to it's last line of code and executes return, the interpreter looks for the most recent return address on the stack and jumps to that point, which will be the second console.log() statement inside of a(). When c() gets to it's last line of code and executes return, the interpreter looks for the most recent return address on the stack and jumps to that point, which will be the second console.log() statement inside of b(). Then b() calls c("fred") and the process is repeated. Again, the interpreter pushes another return address onto the stack so the interpreter knows where to continue executing when b() returns and it sets up the executing environment for the function b() and it starts executing b(). Then, it sets up the executing environment for the function a() it starts executing a(). When the interpreter calls a(), it pushes a return address onto the stack so the interpreter knows that when a() returns, it should go and execute the next line of code after where a() was called. ![]() So, let's say that your main.js at the beginning of your nodejs program looks like this: function a() `) This just controls normal execution of one piece of Javascript. The call stack literally has nothing to do with the event loop. Each function that is called pushes something onto the call stack so that when it returns, the interpreter knows where to go. ![]() The call stack represents the current thread of execution in the Javascript interpreter. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |