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

The "Timer 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 timer queue by beginning to walk through code that demonstrates how the event loop prioritizes certain queues over others.

Preview
Close

Transcript from the "Timer Queue" Lesson

[00:00:00]
>> Will Sentance: The event loop is very strict. What rules does it set for what code to run next and when those functions are allowed back in. Lets get started, line 1, Rumen what are we doing, what are storing in our global memory.
>> Roman: Declaring a function that use imported tweet.

[00:00:14]
>> Will Sentance: Yep use imported tweets, inserting a function that use, imported tweets use imported tweets there it is store function. Excellent thank you Roman next lines Zep.
>> Zep: We store the function definition immediately.
>> Will Sentance: Yeah, there it is immediately. Next line, Sam?
>> Sam: Store the function definition print hello.

[00:00:48]
>> Will Sentance: Print hello, excellent. Next line, Virginia?
>> Virginia: I just throw the function definition black for 500 miliseconds.
>> Will Sentance: Fantastic. Thank you to Virginia.
>> Will Sentance: Perfect, four functions. You know we have to deal with complexity when I have four functions, because I try and minimize this. Okay, now we get started.

[00:01:14]
>> Will Sentance: Well, hold on. Set time out, that sounds a lot like regular JavaScript. Nah, even in the web browser, set timer is not JavaScript. There's no timers in JavaScript. In the web browser, it happens in the background features of the web browser, known as browser APIs. In Node, we had to rebuild those from scratch.

[00:01:34]
And one of them is the ability to set up a timer. Now, exactly how timers are run in Node is using, well, what are timers? In general, timers are just a time in the computer clock and comparing a future time for how many milliseconds past to see whether or not it's gonna take the past around the thing which you delete by assign.

[00:01:56]
So would of call of the time nobody is technically doing that in the background is setting of start time with that function was which it set time out, set up the timer. In the background, and then checking actually loop UV, and actually technically it's the event loop that checks every time, has that timer and not part time pass such that timer would want to run.

[00:02:17]
Sorry that timer is complete and the associated function wants to run. So set time out, takes in a function that you wanna delay, x number of milliseconds. How many here zero milliseconds. Is that happening in JavaScript? No, so let's get going set, time out, it's happening in the purple area known as Node.

[00:02:43]
Set time out passes first, inputs the first argument of set time. Said time out. By the way we find the docs of it on the node docs is a noted feature built in C++ with the help of LUV. Okay, so the second arguably policies number of milliseconds want a delay.

[00:03:01]
Where did you say Zep, set time it's gonna have its consequence.
>> Zep: In node.
>> Will Sentance: In node, so set up in node. A timer with that function and that amount of milliseconds delayed, there it is, timer and zero millisecond delay. And what function to auto run when it's done, is it?

[00:03:23]
>> Zep: [INAUDIBLE]
>> Will Sentance: Print
>> Zep: Hello?
>> Will Sentance: Hello yes good it took us six hours for a British impression to happen, excellent thank you. All right now the function, I got a new little piece here called status. Status is if at this moment, is their activity or has it finish so zero let's say we are setting this as a roughly zero millisecond, at zero millisecond.

[00:03:56]
Zero millisecond is this timer completes, so zero millisecond timer it's you complete.
>> Sam: Yes.
>> Will Sentance: Yeah. So what's gonna be triggered to run?
>> Sam: Print hello.
>> Will Sentance: Print hello, do you think it's gonna be out on the cool stock? Seems reasonable, do you think it is, no. Anything, don't say of course not except obviously border line rational, and nah, it's not.

[00:04:22]
You want a really strict rule, you want a really strict rule. Anything that's being passed into Node Any function that's been set to also run, not by us, we're not running it right? We're relying on node to run it and put it into JavaScript again, that stuff all gets priority after all regular Java code is run.

[00:04:44]
So where instead, it doesn't just sit there though, where instead does it go? It goes to our first queue. The timer queue. Interesting, goes into our first queue the timer queue. And at zero milliseconds roughly,
>> Will Sentance: We get the function. Print hello into the timer queue. So all the set timeout ever does is tell you the time it will take before the function gets put in the queue, not when it's gonna get run.

[00:05:24]
Cuz I have a strong feeling there's a bunch of other stuff that's gonna get priority before this function. Ever a lot of time in queue to execute in JavaScript, it's gonna sit there, okay.
>> Will Sentance: Good, we don't really set ourselves up for that nice. Next one we're back into our good old familiar node built-ins.

[00:05:44]
Now, technically of course we have to add in FS using require that we'll grab and give us the access to nodes FS feature where we're gonna use it. So here we go. I'm just using these numbers here as ordinal. That means the order that matters. It's definitely not a millisecond later.

[00:06:01]
You've got a nanosecond later. But I just want to show you time progressing one millisecond later. We're going to run fs.readfile and pass it. The two important things we need to know where the file is known as a path. And what function to auto run, there' a call back function, what function we want to have node automatically run for us.

[00:06:30]
Fs.readfile. What does it do in Note? Michael. Sets up an instance of the FS feature of Note.
>> Michael: Yes, to access the file system.
>> Will Sentance: Absolutely, with the help of LibUV, and actually sending a background thread to handle the passing of the data and focus on the data coming in.

[00:06:57]
Yeah, it needs to know what path from a dot means the current folder in which her running Node,we always had to run node in an existing folder. We run it in the same folder as we say the tweet. So dot means that slash means do in this folder.

[00:07:11]
Go and find tweet stop. Jason. Okay, at one millisecond Is it done. Now it's going to take a few milliseconds to get that data out. So it's not done right now. But when it is done, what function is going to auto run Andrew?
>> Andrew: Using party tweets.
>> Will Sentance: Fantastic.

[00:07:33]
Exactly. We know the URL and we know the function is going to auto run. It's gonna run US imported tweets. There it is. I'm just gonna separate this out so it's clear.
>> Will Sentance: Here we go. So, we are now using the FS feature and the timer feature in the background.

[00:07:55]
Two auto run functions set up and ready to go already. Okay, the data is gonna start coming through the background thread's handling the data assigned to come through, but at one millisecond, it's definitely not ready. We definitely have the tweets ready to go. We're definitely not auto-triggering using for the tweets, not yet.

[00:08:18]
In JavaScript, we now hit our, did I miss anything there, by the way? I don't think I did, yeah. In JavaScript, we now hit at two milliseconds again roughly two milliseconds. This is not in nanoseconds, it's much much faster but just for our symbolic purposes, we are going to hit cooling block for five hundred milliseconds.

[00:08:41]
Lock for 500 milliseconds. We're not going to describe how it works. We just know that as the thread enters it, it gets added to what? Sam?
>> Sam: Cool stack.
>> Will Sentance: Cool stack, whose side is pushed to the cool stack. Cool for 500 milliseconds, my energy so hit I was it wasn't a question.

[00:09:02]
It was me trying to convince myself. Why are you still here?
>> Speaker 8: [LAUGH]
>> Will Sentance: I'm getting similar questions but, okay, good, I like it, that's actually very inspiring to me, thank you, Michael. There it is, I head into it, it's gonna sit, therefore last 500 milliseconds, how? Not by setting up timers down here, nothing to do with timers.

[00:09:21]
That's on in the background, it's right. How did I come up with this foreign minister. I tested it out is roughly adding five million elements to an array. And the only way you can make something take long in JavaScript is jobs will let you do stuff it takes a long time because it would be really be it nobody's gonna block any further code.

[00:09:35]
So any long term titles, it slowed like a timer is auto delegated to the background. The only way to trick it into taking a long time or blocking the thread from running further is to do a task like, lots, lots of tiny tasks like accessing an array, adding an element.

[00:09:54]
Try to give the awesome ways to do stuff and block it in the thread here. Like the sink. He saw the reed fall sink that you will use in the challenge. That was actually setting up the file system access here and waiting in JavaScript until the data came back and assign it in JavaScript.

[00:10:09]
But that was absolutely unused in practice we do not block. The only way we can do it typically is by doing something silly like a very big or silly. But you know like finding an enterprise number or. That would block and that's what we're doing.

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