The Hard Parts of Servers & Node.js Asynchronicity in Node Q&A
Transcript from the "Asynchronicity in Node Q&A" Lesson
>> 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.
>> 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?
>> 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: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?
>> 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: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.