Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Check Queue" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will introduces the check queue and demonstrates how setImmediate adds function calls to the check queue, as well as in what order queued elements will be run.


Transcript from the "Check Queue" Lesson

>> Will Sentance: The third prioritized queue is called the Check Queue.
>> Will Sentance: Let's just squeeze it in a bit here. The check queue.
>> Will Sentance: Check queue, how do I get a function in the check queue? I use set immediate. Whatever function I pass is set immediate, that function will be absolute opposite of running immediately.

It is the worst named function in all of history. That's about ten years, and it's the worst named method in all history. It absolutely, you're so glad is, that it certainly is not gonna run immediately. It's literally certainly gonna be the last queue to be checked.
>> Speaker 2: [LAUGH] why?

>> Will Sentance: It is the worst, it is a misnomer beyond. But, there are times when we want to make sure that all IO work, at that point, has been done. All completed input, output, auto run functions have run. And we wanna be sure that they've all run, before we run this other function.

Another way we can do that, is by using a set immediate that will put the associated function in the last queue. As opposed we've added something from one of these into a global variable, we wanna be sure it's already there, and it's gonna give as a right to control the order of our execution.

Of functions that we auto-delay, it's kinda nice that we can actually insert something from the running of this function that will set immediate that will go in here. That we can then know all of this would've finished, and we can then check this queue lost. And when that queue is checked, that function, we know that all IO related delayed functions that have been auto-run have been run at that point.

Not all ever, just all have it done at that moment, okay.
>> Will Sentance: All of that in cycle of checking through the associated queues, which happens again, and again, and again. In that cycle of checking, all currently finished call backs in the queue have finished at that moment. Then we enter the check queue.

And so, at five and into five minutes, three minutes seconds, roughly I guess, this again is not real milliseconds, just ordinal, ordinal means the order of numbers, so that was the next thing that's done. At 503 milliseconds, we run set immediate, we run set immediate. Pass it, what function?

>> Sam: Immediately.
>> Will Sentance: Immediately, the whole definition of it. I should've call it not immediately, right? That would've been much clearer. Immediately, and that also speaks to node.
>> Will Sentance: That is not true, it has gone the other way round. There it is that speaks in a that can go the other way around others, clearly.

And that's going to set up down here, a,
>> Will Sentance: Well, we can represented here, what is represented here directly. You know what, it does have any manifest difference between whether we put it down here, I mean just to be consistent. It's setting up a C++ background feature, so let's put it in here.

The immediate slash checks because they going to check queue,
>> Will Sentance: The function immediately. But, it's instantly grabbed and put in the check queue at 503 milliseconds.
>> Will Sentance: 503 milliseconds. And you might be thinking, hold on, how these still in there? Because we still were running through the whole way through this regular jobs synchronous code.

So we won't even allow, the event it wasn't even checking any of these queues. Even those very last thing we had to say immediate, is gonna be added in here. We still haven't grabbed anything else from our queues and this is our third priority cues. I feel like it might be the last thing to be checked.

All right, so, now, is there any global code left? This was popped off the call stack a long time ago. Is there any global code left?
>> Speaker 4: No.
>> Will Sentance: Is there any thing when I call stack.
>> Speaker 5: No.
>> Will Sentance: Exactly. So, event loop goes, we're good to check the queues.

And we check the queues at, I guess, again, metaphorically, 504 milliseconds we start checking queues. And what's the first queue we check?
>> Speaker 2: Timer queue.
>> Will Sentance: Timer queue. It's actually technically what's called a timer minheap. The way it's represented is, the way of organizing data where you take the smallest counter is invested number of seconds, it's been delayed and check that one first.

And if it's below the amount of current time, then it grabs that associated function inserted on the cool side. But we can metaphorically think of it as a acute. Just, shoot lasers better organizes and what's called a minheap, which is a better way of organizing certain data. But we'll call it, it's a queue effectively, and we do indeed at.

504 milliseconds, put print hello, on the call stack, my goodness, print hello is executed and what did it do in our console people, in console logs, hello at 504 milliseconds. Hello, thank you, Sam for great enthusiasm. They're so enthusiastic to get a drink of water after the strength of the hello.

All right, and it's the cube. And the event that goes, check the next queue, which is where 95% of our delayed functions to be auto run will be in here. Because that's what will be interesting stuff is time isn't all that interesting. Sammy there's a lot I enjoy while they're quite cool actually.

But you know that's not a go to thing all, stuff is interacting. IO, note it is most fun when it's interacting with the computer's internals is what they first 70 percent is taught wall which it was, right? They all go in here as why this last bit here is almost like an appendix.

It's like most of the time. You're not gonna worry too much about this is great for interviews, and for really, really interesting and deep debugging. And that's why we're gonna cover it and we're almost there. But it's not like the cool fun, but it's using the features of the computer via C++ in JavaScript.

Here and now, you doing weird stuff like delaying functions, timer, zero minute to make the point of exactly how the queues allow stuff back in on the event loop via the live event loop.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now