The Hard Parts of Servers & Node.js

Microtask & Close Queues

Will Sentance

Will Sentance

The Hard Parts of Servers & Node.js

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

The "Microtask & Close Queues" 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 microtask and close queues, and explains when the items within will run compared to those in other queues.


Transcript from the "Microtask & Close Queues" Lesson

>> Will Sentance: We are not gonna talk about promises today, but if you watch, Async: Hard Parts, you've heard about promises. These are an alternative way of setting up work to happen in Node with C++. Where rather, rather than a function being auto-run in JavaScript, data will be auto-updated in JavaScript that will trigger a function to run on that data.

That function is not added to any of these queues. It's added to something called the microtask queue, which takes precedence over each of these queues. It's priority number zero. Sort of zero indexed. Not only that, it actually goes back and checks that queue between each of the checkings of the other queues.

I'll just put it in, we're not gonna go into great detail. But the microtask. And actually, there's two of those.
>> Speaker 2: [LAUGH]
>> Will Sentance: So 0 to 0 is the microtask queue. I guess we could have a and b. The first one is if we run a function which we're not meant to use anymore, called process.nextTick, and pass it a function, that one gets stuck in this very first queue here.

Any functions delayed using promises, they get stuck in this one here. And in between the event loop checking this, this, this queue, it will always go back and first check,
>> Will Sentance: These microtask queues before it moves on to check the next queue.
>> Will Sentance: That is now all, there's one final queue we're not gonna touch on.

>> Speaker 2: [LAUGH]
>> Will Sentance: There's one final queue, which is called the close queue, which is on any close events, remember those down there, the ones that could trigger a function. Those associated functions go into a fourth queue called the close handle queue. And any functions that were set to auto run on the closing of a stream, the finishing of accessing data, they will be added in this close queue.

Six queues that the event loop prioritizes one, two, three, four, zero, it's the most important of all. And actually, it checks these two in between checking any of the other four queues. And when it's gone through one cycle, [SOUND], back again, it goes again. Back again, it goes again.

Back again, it goes again, as long as your computer, as long as your Node app is still turned on.

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