Check out a free preview of the full The Hard Parts of Servers & Node.js course:
The "Introduction to Asynchronicity" 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 emphasises the importance of knowing in what order Node is executing queued operations and why, and sets up the section to examine how Node's event loop, stacks, and queues work. - https://static.frontendmasters.com/resources/2019-04-24-servers-node-js/Hard-Parts-Servers-Node.pdf

Get Unlimited Access Now

Transcript from the "Introduction to Asynchronicity" Lesson

[00:00:02]
>> 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.

[00:00:39] I've got this, written up here, look. The call stack, event loop and callback queue in Node. Call stack, JavaScript keeps track of what function is being run, it'll be top of 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:01] As any delayed function will be running global, any autorun function will be running global. Whenever a function is to be run, it's added to the call stack to tell JavaScript, run me. Callback queue, any function is delayed from running, I mean, we delay them by storing them down here.

[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:01:47] Okay, but there's actually a whole bunch of queues, and different functions set to autorun by Node will be put in different queues. And it's really restrictive, the event loop, on what has to have finished in synchronous, regular JavaScript code before anything from those queues is allowed onto the call stack.

[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:03:49] I'm then gonna run this block for 500 milliseconds function, which we're not gonna write the code of, but when it runs, it is not a timer. It is definitely not a timer. It is gonna do some task in JavaScript billion times, well, actually for about 5 million times that will end up taking 500 milliseconds in JavaScript, not in Node, but in JavaScript.

[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.