These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
This lesson dives deep into the guts of Node.js to illustrate how the Node event loop works. By the end of this lesson, you'll understand what makes Node.js asynchronous and why it is referred to as having non-blocking event-driven I/O.
A call stack is many times referred to as "the stack". It stores data about procedures that are currently running in a given program.
...the main reason for having one (a stack) is to keep track of the point to which each active subroutine should return control when it finishes executing.
A stack is also a data structure that stores items. It is a LIFO data structure in that the last item to be stored (pushed) on the stack is the next or "first" item to be removed (popped) from the stack. Why is this significant?
There are various places for stacks and their counterparts queues. Basically, they are important because they store procedures so that they can be executed in an organized fashion. So how do stacks and queues work in the Node.js event loop?
Before we dive into what the Node.js event loop is and how it works, we need to identify the various parts of the whole.
libuv
library: libuv is a multi-platform support library with a focus on asynchronous I/O. It was primarily developed for use by Node.js.The Chrome V8 engine is responsible for executing JavaScript code. It can actually be embedded into a C++ application which is what Node.js is at its core. The V8 engine takes in JavaScript as a string, executes it and then prints the result to STDOUT. However, there is an issue with this because JavaScript is synchronous. So how does Node.js allow V8 to execute synchronous JavaScript code while performing asynchronous operations?
The following diagram illustrates the communication between these various pieces of Node's event-driven I/O.
The diagram can be broken down into these steps:
libuv
library submits a request to the OS to perform the taskBy viewing this diagram and understanding these steps, it is easy to see that the Node.js event loop is exactly that: it is a continuously looping process that is checking to see if other processes have completed. Let's examine the code from the previous lesson again:
var fs = require('fs');
var path = './data/lorem.txt';
fs.readFile(path, 'utf8', function(err, data) {
console.log('Async!');
err ? console.error(err) : console.log(data);
});
console.log('Done!');
It should now be more clear how the program logs "Done!"
before it logs the contents of the file. The V8 engine continues to execute the JavaScript outside of the callback and when the asynchronous task is complete the Node.js event loop notifies V8 and runs the callback.
Having a solid understanding of the Node.js event loop is key to forming a clear mental model of how Node.js works. You now have been given a tour of the various major pieces of Node.js and that which enables it to have event-driven non-blocking I/O.