Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "IO Queue" 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 introduces the IO queue by further explaining the event loop's prioritization system, diagramming the addition of calls to the timer and IO queues.


Transcript from the "IO Queue" Lesson

>> Will Sentance: And about, I don't know, let's say about 200 milliseconds in, about 200 milliseconds in, with 200 milliseconds in, what's gonna happen down here? Just by the size of this file at 200 milliseconds, the data comes back. We're thrilled, don't even start how happy I am right now, I'm over the moon news.

What functions is gonna auto run presumably straight back in JavaScript, get rid of block for 500milliseconds? It's not gonna continue running, hm, poor, of course, it's gonna continue running. So instead, well, what function is gonna auto run at this point, Sarah Rose, when that data comes back without tweets?

What function is gonna auto run, Michael?
>> Michael: Use imported tweets.
>> Will Sentance: Use imported tweets, there it is, there's our function. What data is gonna be auto inserted, auto insert data, what data is gonna be auto inserted, Sam? When the data comes back from the fall system, what data is gonna be auto inserted?

>> Sam: That's the two parameters of the deal.
>> Will Sentance: Two of them, yeah, yeah, yeah, yeah, the-
>> Sam: Data the error, and then the data.
>> Will Sentance: The error data, and then the actual data, exactly and so, we've got the first one error data is that hope null. And the second one is the actual data which would be actually totally a buffer which is there isn't one format, but we can say for now, we can say JSON string, JSON data.

Tweet 1, Hello and so forth, there it is, they're ready to be inserted, the function's ready to run. Is it allowed onto the call set to run? Definitely not, our event loop as introduced the event we began, our event loop is very strong. Very strict, doesn't allow anything on if there's anything on the call stack or any further global code to run.

Mm, mm, I'm not allowed back home, so instead, we have a second queue. It's called the I/O queue or I/O callback queue, so this is what we actually saw earlier. And into it, a stored, queued up any functions triggered to run, by honesty, most of node. Like 95% of the functions, you're gonna have autorun, will end up in this queue.

Anyone's involved, data come from the file system, from a network socket, any of those, all the associated autorun functions go into the I/O callback queue. Okay, so what one comes in, just to help me out, Bevette again, which one's gonna be stored in callback queue to autorun? For our tweets come in Zed?

>> Zep: Use imported tweets.
>> Will Sentance: Use imported tweets, there it is, use imported tweet, and there it is at roughly 200 milliseconds. It gets added to that queue, okay,
>> Will Sentance: Well, it's sitting there, at 502 milliseconds, what function finishes on our call stack, Sam?
>> Sam: Block for 500.
>> Will Sentance: Block for 500 milliseconds, does the event loop feel confident yet to add to go look in the queues?

No, why not, Sam?
>> Sam: There's some more code to run.
>> Will Sentance: There's still more global code to run, the code that we determine to execute, that always comes first. And what is the next thing that, 502 milliseconds be run, Sam?
>> Sam: Console log me first.
>> Will Sentance: Fantastic, console log me first and there it is, 502 milliseconds.

But our queues is only to fill up with stuffs, they're sitting there, very unhappy. But there's still more global code to run and these ones are really, really intriguing one. This is a feature of node to ensure that we can add a function to run after all I/O input/output functions have definitely run.

All I/O functions are finished running. We can be sure that all associated I/O functions at that check of the event loop will have finished running, because it's the third prioritized queue.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now