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 will cover the core concept of what asynchronous programming is and how it ties into JavaScript and Node.js.
In order to understand what asynchronous operations are it can be good to review what synchronous means.
Synchronous means to occur or exist at the same time. This is a familiar concept if you think about the act of synchronizing your watches or synchronized swimming.
So what is the difference between synchronous programming and asynchronous programming? It comes down to waiting or not waiting.
Think about the act of performing the movements of synchronized swimming. They must all be performed together. All swimmers involved must proceed in the same direction, swimming at the same pace, and stop at the same time. One swimmer cannot simply complete the routine before another or the effect is ruined. This makes sense for the swimmers because it is beautiful to see the combined movements working together. However, what if the goal was to allow faster swimmers to complete the routine before the slower swimmers? This is where asynchrony gains its advantage.
Now that we've talked about its opposite, let's address asynchrony. Asynchronous operations don't wait for other operations to finish before proceeding. Instead, they continue and allow other operations to finish. Once these operations are finished they can choose to callback the original process and inform that process that the procedure is complete. This means that these different processes will be carried out on separate threads.
There is an excellent explanation of the differences between asynchronous and synchronous programming and how it ties into threaded processing here on Stack Overflow.
But how does asynchronous programming get used in Node.js?
You will often hear that Node.js is asynchronous. You'll also hear that Node.js uses non-blocking event-driven I/O. All of this sounds very impressive, but what does that mean?
By now you should be very familiar with the concept of executing code line by line. The first line must finish executing before the second line can start. This is synchronous. This is, in fact, the way JavaScript runs! JavaScript is actually synchronous! So how is it that Node.js, which runs JavaScript, can perform asynchronous operations? Let's examine some code briefly.
var fs = require('fs');
var path = './data/lorem.txt';
var data = fs.readFileSync(path, 'utf8');
console.log(data);
console.log('Done!');
The code above accesses a file and logs its contents to the console. The line that outputs "Done!"
to the console will not be executed until after the preceding line that outputs the file's data. The call to fs.readFileSync
waits until the file contents are loaded and returns them to be assigned to data
. Each line in the above example waits for the previous to finish before executing. For this reason, we know for a fact that "Done!"
will not be output before the data from the file. However, consider the following code that uses the asynchronous approach.
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!');
The above example uses the asynchronous method to access the file's contents and therefore is passed a callback function. This callback function is executed "some time" in the future, or asynchronously from the current thread when the file content is loaded. This means that we cannot know for a fact that "Done!"
will be output after the file contents. Because the first thread continues immediately, it is more likely that the first thread will complete before the file contents is returned and result in the file data being logged after "Done!"
. This is why Node.js is referred to as non-blocking and event-driven.
This lesson was meant to give you background about what asynchronous programming is and how it applies to Node.js. However, we've only begun to scratch the surface of how it is used in Node. Next, we'll dive into the inner workings of what makes Node.js non-blocking and how events and JavaScript execution are managed asynchronously.