Made in

Ship Your First Personalized Web Experience webinar on June 15 @ 10AM PT. Register Now

Talk to Us
Talk to Us










Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required



Get StartedLogin

‹ Back to blog

Web Development

Visualizing the Close Queue in the Node.js Event Loop

April 17, 2023

Written By Vishwas Gopinath

Welcome to the seventh and final article in our series on visualizing the Node.js event loop. In the previous article, we explored the check queue and its order of priority when executing asynchronous code. In this article, we will take a look at the last queue in the event loop, which is the close queue.

Enqueueing callback functions

Before we examine our final experiment in this series, I want to point out that the close queue contains callbacks associated with the close event of an asynchronous task. Here's an example of adding a callback function to the close queue:

readableStream.on("close", () => {
  console.log("this is from readableStream close event callback");

All the previous experiments dealing with other queues have been covered in the previous articles. They are run using the CommonJS module format.

// index.js
const fs = require("fs");

const readableStream = fs.createReadStream(__filename);

readableStream.on("close", () => {
  console.log("this is from readableStream close event callback");
setImmediate(() => console.log("this is setImmediate 1"));
setTimeout(() => console.log("this is setTimeout 1"), 0);
Promise.resolve().then(() => console.log("this is Promise.resolve 1"));
process.nextTick(() => console.log("this is process.nextTick 1"));

First, we import the fs module. Then, we create a readable stream using fs.createReadStream(). Afterward, we close the stream using the close() method. We listen to the close event, which is emitted when the stream is closed, and add a listener to log "this is from the readableStream close event callback".

After this, we have a few familiar methods: setImmediate(), setTimeout(), Promise.resolve().then(), and process.nextTick().

After the call stack executes all statements, there will be one callback in each queue except the I/O queue. When there is no further code to execute, control enters the event loop.

First, the callback in the nextTick queue is dequeued and executed. Then, the callback in the promise queue is dequeued and executed, followed by the timer queue callback.

Next, the event loop moves on to the check queue and dequeues and executes its callback. Finally, control moves on to the close queue, where the final callback is dequeued and executed.

Close queue callbacks are executed after all other queue callbacks in a given iteration of the event loop.

The event loop is a C program that coordinates the execution of synchronous and asynchronous code in Node.js. It manages six different queues: nextTick, promise, timer, I/O, check, and close.

To add a task to the nextTick queue, we use the process.nextTick() method. To add a task to the promise queue, we resolve or reject a promise. To add a task to the timer queue, we use setTimeout() or setInterval().

To add a task to the I/O queue, we execute an async method. To add a task to the check queue, we use the setImmediate() function. Finally, to add a task to the close queue, we attach close event listeners.

The order of execution follows the same order listed here. However, it's important to note that the nextTick and promise queues are executed in between each queue and also in between each callback execution in the timer and check queues.

Visually build with your components is a headless CMS that lets you drag and drop with your components right within your existing site.

// Dynamically render your components
export function MyPage({ json }) {
  return <BuilderComponent content={json} />

registerComponents([MyHero, MyProducts])


Hand written text that says "A drag and drop headless CMS?" is a headless CMS that lets you build visually with your components.

Learn more

Like our content?

Join Our Newsletter

Continue Reading
Web Development11 MIN
Next.js 13 - Routing Fundamentals and Beyond
WRITTEN BYVishwas Gopinath
June 9, 2023
Latest News7 MIN
Builder Drop: More insights, environments and localization permissions
WRITTEN BYBuilder Team
June 8, 2023
Latest News6 MIN
The Dev Drop: Multi-threading in JSX, Resumability, Qwik Case Study, React in 2023
June 1, 2023