So ABC is winning right now. Okay, so let's... Cost of node.js lifecycle. Okay. That's true. I think, yeah, the person that guessed ACB is right. Let's take a look. ACB. So first of all, the order is the same. Again, you need to think about this as, there's a single thread and there's a queue of instructions. And the queue of instructions is the same with the only difference that before printing out C we have sleeping for two seconds. But everything else is the same, right? So the order of locks must be the same too.
What is interesting here is that we are waiting two seconds between A and C and then B is printed out right after, almost instantaneously. So what we see there is that there is no an extra seconds delay after we slip for two seconds. Like this is what set timeout does, it's letting me describe a delay that is starting from the last run instruction, well, last encued instruction. So since we are slipping for two seconds, two seconds is more than one. It prints out b instantaneously, it doesn't need to wait more. Okay. Cool, so now let's go to the third example. And here we are not going to block the thread or anything like that, but we are setting the timeout to zero milliseconds. So in your opinion, is this going to change anything? Is the order going to be affected by this? Or yeah, make your bets, I guess.
Okay, so ACB it won't change, still the same. ACB as well. Yeah, so you're right, basically it's not going to change. ACB again, yeah. So you already understand how this queue thing is working. I am happy about it. So ACB, the order is the same, the fact that we are not waiting for any millisecond at all doesn't change the fact that this is enqueuing the log first, this is enqueuing the set timeout call, and this is enqueuing the log C. And then when this is called, you can teach using stack mode, yeah, exactly. When this is called, this is enqueued at the last position, and then, like, yeah, so the stack, by now, has A, now the stack has A, and the setTimeout, I'm just improvising here, and then here, the stack has setTimeout, and then C, and then when this is executed, like, the engine starts, actually, to take instructions and run them, it will take A, and the stack has setTimeout, and C, then it takes the setTimeout, and runs it. And the stack has C, and as a result, the stack now has C and B, because the execution of the setTimeout enqueues this call here. And then, finally, C is taken, B still pending to be run, and then B is taken, the stack is empty, and the program ends. Oh, okay, I hope this improvised. I wasn't planning to explain it this way, but I hope it makes sense. So, anyway. Uh, cool. Yeah, again, the same, I'm just, you know, we are dancing around the fact that there's only one thread, and there's a queue of instructions to be run, and setTimeout is really, you know, it has this very misleading name of timeout when in fact, it's enqueuing stuff with a time-based trigger, or I don't know. The mental model I follow, that the way I think about asynchronous behavior in JavaScript in general, has to do more with enqueuing stuff like we did here, and understanding what is going to be the trigger about. In the case of a setTimeout, it's going to enqueue like it happened here. It's going to enqueue B, but it's B with an asterisk because it's not like the interpreter won't take B out of the stack if not enough time has passed because maybe I need to wait for seconds. If not enough time has passed, B is going to stay in the stack until that time has elapsed and then it's going to execute it once that time has passed. And this is exactly the same when we are talking about a night AJAX call, for example, it's not a time-based trigger. It's a trigger that is based on some input output operation that happens in the network layer when some request is done to some server and then the operating system gets the response and parses it, blah, blah, blah. And then the trigger is like my callback for the AJAX call is going to be that I have the response and the response has to be parsed and then the callback is called. And until that response is available, the callback is going to be in the queue waiting to be executed. Or for example, if I'm writing a file in the file system, the trigger will be about that write operation to be completed. And then, the mental mode, everything is the same. They are just callbacks that are in queued. They are in the queue waiting to be executed and they will be executed based on some trigger and depending if I enqueue to them with a set timeout or an AJAX score or a file write. Yeah, those are different triggers that will be taken into consideration when that callback is the next thing to run, right? That's the way I think about these things.
Comments