Transcript from the "Introduction to Asynchronicity" Lesson
>> Will Sentance: This is not per se realistic code, what we're gonna do here. This is a way of us seeing, we just earlier, a moment ago saw a very realistic scenario, where we were dealing with batches of data coming in triggering an event, the data event, triggering function to run associated with that data event.
[00:00:21] And then starting to kinda queue up as it was taking longer to run them in here than it was taking for the batches to come in. That's fine, that's very realistic, that's fine. And we discovered the event loop was in charge of determining what function was next allowed to run on the call stack.
>> Will Sentance: And where it was run from, in other words, in which function was it run, or was it running global?
[00:01:21] And therefore, autorun by Node are added to the callback queue. When the background Node task, or in other words, computer feature has completed, or there's been some activity like a request, the associated function that's triggered gets added to a queue of functions for the callback queue. The event loop determines what function, in other words, code to run next from the queues to pull into the call stack and execute.
[00:02:13] And so we're now gonna see code. Node is most powerful because of the automated JS function execution triggered by Node at just the right moment. It means you don't have to wait. Okay, but it also means we better know intimately how Node decides what to automatically execute at what moment.
[00:02:28] And so we're gonna set up a scenario here, 1, 2, 3, 4, 5, 6, 7, 8, 9 lines of code, four functions we're saving. And we're gonna delay three of them by using Node C++ features, attach functions to be autorun. And we're gonna see that each of the functions will behave differently.
[00:02:50] In other words, when it's triggered and ready to run, it'll be added to a different queue. And the event loop will prioritize certain queues over other queues. And this is not a realistic scenario. I'm gonna delay a function print Hello by 0 milliseconds. Why am I doing that?
[00:03:07] Because I want you to see just specifically how all of these, exactly how the event loop works and how each of the queues works. Not because you're actually gonna do this in practice but because I want you to see just how might this work in an artificial scenario, to know all the elements of the queues and exactly how the event loop works.
[00:03:27] So we're gonna save four functions. We're then gonna set a print Hello function to run after 0 milliseconds. I already have a feeling it will not run after 0 milliseconds. I'm gonna set up a function use imported tweets to run after a bunch of tweet data has been imported, the whole file, using read file.
[00:04:15] What sort of thing could we do, Sam, that would maybe take 500 milliseconds?
>> Sam: Loop through a giant array?
>> Will Sentance: Yeah, loop through a giant array, add elements to an array, add 50 million elements to an array. I think roughly I found it was 5 million elements being added to an array took about 5 milliseconds, roughly something like that.
[00:04:34] Okay, 500 milliseconds, sorry, 500 milliseconds. So we're gonna run that. Then we're gonna run a console log. Then we're gonna run this funny thing called setImmediate, which is another Node feature that gives us control over putting stuff into another totally separate queue when, well, we'll see, we'll see.
[00:05:02] We're gonna see all the queues. Well, we're gonna see, how many of them, one, two, three, one, two, three. Two of them we're not gonna see today. You might see it another time.
>> Will Sentance: Yeah, we're gonna see three of the queues, enough to realize the overall model of how Nodes execute.
[00:05:25] And I will tell you the other ones as well so you're aware of it. We're not gonna play them out in full.