Check out a free preview of the full The Hard Parts of Servers & Node.js course
The "Asynchronicity in Node Q&A" 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 fields student questions about require, the structure of the code that makes up the event loop, and the queue data structure.
Transcript from the "Asynchronicity in Node Q&A" Lesson
[00:00:00]
>> Will Sentance: Andrew, we'll start with you.
>> Andrew: You mentioned that we have to require a bunch of things when we use them. Is it anytime we use a Node label, we have to require-
>> Will Sentance: Yeah, but not this one and this one. We do not need to require setTimeout in, we do not need to require setImmediate in for the rest here, yeah, yeah.
[00:00:21]
And we can see a list of all the labels we need to require in in the Node docs. And by the way, some of them, behind their scenes, are requiring in, well, they're requiring other stuff, but they're setting up other Node stuff like HTTP. When we use HTTP, behind the scenes, we're using Node's net feature, which is more generic setting up of a socket to the net, to the Internet.
[00:00:43]
Excellent, Andrew, Michael.
>> Michael: Okay, so I feel like I wanna go further deeper into the matrix here, a little bit.
>> Speaker 4: [LAUGH]
>> Michael: So if I were building, okay, what is the event loop in one of these callback or stacks, right?
>> Will Sentance: Wait, wait, so queue, yeah, queues.
[00:00:57]
>> Michael: So if I were building an event loop, I would just write a while statement that never ends.
>> Will Sentance: Fantastic, wow, and in fact, this is Boolean C++. And we can go and look at the C++ code and it says, while there are still things that could in the background, what an outstanding question by Michael.
[00:01:22]
What language do you come from, Michael?
>> Michael: JavaScript.
>> Will Sentance: Okay, so Michael just asked a question there. Could I build my own event loop? Absolutely, all of this is a while loop that first confirms, is there anything that could still be autorun? Cuz if there's nothing that could be auto run, there's no point continuing the looping to check it.
[00:01:44]
There's also a run function. You just end JavaScript application. That's why if you ever see where you haven't set up a background feature in Node and you run Node on your file, it exits out, right? If you ever write Node Server.js and you've not done anything in it, it runs and then next line, the command lines started again, because you finished your Node app.
[00:02:02]
But as long as you set up something that in the background could still autorun a function, then the while loop will go, while there's still stuff that could be autorun, continue to cycle to check first this queue, then this queue, then this queue. Then go back and check these queues, then check this queue.
[00:02:24]
Then go back and check these queues, then check this queue. Then go back and check these queues, then check this queue. And then do it all over again. Yeah, go ahead, Michael.
>> Michael: So then if you're writing a queue, is it just a function that's towards other functions to be run?
[00:02:38]
>> Will Sentance: Well, I'd imagine you could implement it. Some languages have direct implementation of a data structure, a way of storing data called a queue. Data structures are described by what they can do. So what can a queue do? Well, you can dequeue the first element out of it, and then the next that if you try to run dequeue on it again, it will be the next thing in it.
[00:03:00]
So data structures, ways of storing data, their character, their behavior, is described by what you can do on them, not by necessarily per se how the data's stored. That's part of it, but it's also by what you can do with them. So with a queue, all it is, you could think of it in JavaScript, you wanna build a queue, you'd have an array, yeah, and then you take the first thing and you can have an array of functions, no problem in JavaScript.
[00:03:23]
You take the first element out and you better make sure you shift all the previous elements to the next. Now if you were to use in JavaScript, you can imagine building a function very easily that took out that first element, first function, shifted all of them down to the next index.
[00:03:39]
Next time you run that function, you get the next function out. That's all it is. You could rebuild your own mini-event loop as a manifest. That's very cool question, Michael. What a great way to close.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops