The Hard Parts of Servers & Node.js Event Loop Completion
Transcript from the "Event Loop Completion" Lesson
>> Will Sentance: D cued event loop now checks number two where it finds what functions sitting there ready to run since 200 milliseconds.
>> Speaker 2: It's imported tweets?
>> Will Sentance: Use imported tweets, it was added their long ago automatically. And it's got auto date instead of data. God, anyone tired of doing those?
[00:00:16] Yeah? Yeah, me too, exactly. Add up 505 milliseconds next in line, we auto run which function, Zep?
>> Speaker 2: Import tweets.
>> Will Sentance: And by the way, sorry, this was auto run by Node, so the parens were added by Node. Yes, exactly, now we run-
>> Speaker 2: Use imported.
>> Will Sentance: Use imported, thank you.
[00:00:35] Use imported tweets, do we execute it with parens ourselves? No, who does?
>> Speaker 2: Node.
>> Will Sentance: Node does, and it creates an execution context. There it is.
>> Will Sentance: And the data going in, the first auto created input is the error data made by Node, which is just null in this case, in goes null.
[00:01:06] The second is the data, the tweets, which I'm just gonna do, is actually a buffer of zeroes and ones, which we'd have to stringify, but just so we see what it is. It's a bit easier to spot what it is. We'll pretend it comes in as actual JSON, it actually, as I say, technically comes in as a string of zeroes and ones, a data type called a buffer.
[00:01:41] So they're perfect for streams of data, buckets of, chunks of data going in. Okay, tweet one and so forth, what are we gonna do with it? My God, what's it say to do with it? Parse it and tweet. Let's just log the tweet. Parse it in the local memory.
[00:01:57] That's just to remind ourselves what are our parameters and use imported tweets in the local memory. Who have I not called on for a bit? Virginia, what are our parameters for use imported tweets? What are our parameters?
>> Speaker 3: It's error data and data.
>> Will Sentance: Error data and data, and the first auto input was what?
>> Speaker 2: Null.
>> Will Sentance: Was null, Bhavit, thank you. And that fills in the first parameter error data. The second one was data, again, we could have called it anything, as long as we make access to it cuz we didn't, we chose these names. And the second one has what in it, Michael?
>> Speaker 2: Data.
>> Will Sentance: The actual data, exactly, the actual tweet string.
[00:02:57] And then the final thing we do there is console log tweets, tweet1, which is what, Zep?
>> Speaker 2: High or low.
>> Will Sentance: It's high, exactly, whatever, sorry, it's hello, sorry, thank you.
>> Speaker 2: It's low, yeah.
>> Will Sentance: Yeah, sorry, there you go, hello. [SOUND], find hello, and there it is at 505 milliseconds.
>> Will Sentance: We're almost there, people. Hello again, it's the same thing again, my God. Hello again, this time in lowercase, and that's almost it. It's de-queued, it's, we didn't even add it to the call stack in the first place. Okay, it was on the call stack, I'm gonna do, it was on the call stack, okay?
[00:03:34] We're not gonna put it up again. So this event loop checks its final queue knowing that all i/o callbacks in here are being run automatically at this point. They'll be handled one by one, and we know they're all done so we know that if we want to use some stuff that was achieved in those, maybe store globally, it'll be there.
[00:03:53] And that's when we finally have Node check the check queue, and add, what function was it? What was the function called, everyone?
>> Speaker 2: Immediately.
>> Will Sentance: Immediately, I forgot to write it, is added at 505 milliseconds, we hit the running of immediately, we put it on the call stack.
[00:04:19] It doesn't do much, what does it immediately say to console log, Sarah Rose?
>> Speaker 3: Sorry, it's run me last.
>> Will Sentance: Run me last, and there it is, at six milliseconds, our very last run me last. I think it's quite sad the very final thing I'm gonna write on this board Is a crying emoji.
[00:04:51] But I think maybe-
>> Will Sentance: Quite, there you go. Run me last. All right, people, there we go, people. That is all of well, no, there's a couple of other queues, but we're not gonna touch on them too much here today. There's actually a couple other queues.