Join Alexander Zanfir for an in-depth discussion in this video Understanding asynchronous tasks, part of Up and Running with Node.js.
- When you execute a task synchronously, you wait for it to finish before moving on to another task, which is what causes blocking. On the other hand, when you execute tasks asynchronously, you can move on to another task without having to wait for the previous task to finish. For example, if you've ever used an application or a website where you had to submit the form, and everything went great until the response was received, that's the background process running synchronously and blocking the responsiveness of the user interface until it completes. If we were to make a list of all the things you could do on a computer and ordered them based on their completion time, there would be two that require relatively long waiting times: networking access and file system access.
And with a web app, these are some of the most common functions. This is why Node performs so well for web apps. The most common way to get started with asynchronous code is through callbacks. So let's take a look at some code. Please open up the following files with your editor of choice. We'll be taking a look at sync-demo.js and async-demo.js. And you can find these in your exercise files. Let's start by looking at the synchronous version first. With this app, we're loading in the file system, and then we're reading in the directories from drive c.
Once that completes, it then executes the next line and so on. And so when I execute this, we should see the console.log that displays the directory list, and then we'll see the next console.log which says this comes after. I'll be trying that now, and you could follow along if you like, or just watch as I explain. So the first thing we see is the console.log of the directory list. And as we expected, we see this comes after. So now, what will happen different when we look at our async demo? Let's first just run it.
As you can see, "this comes after" is displayed first, and then the directory list. So let's take a look at the code to see why that occurs. Even though the console.log for this comes after, it is on the last line, it still executes first. And the reason is because of this callback, which I have defined up here as a function, and I pass it into the file system for reading the directory. So instead of the execution waiting for readdir to finish, it continues, and goes to the next line. And then once readdir finishes, it calls this function, which is our callback, and then it executes this console.log.
You might be wondering why I called our callback "phoneNumber." Well a good analogy for callbacks is calling technical support for your internet provider. For most companies, you have to wait on hold, and put your day on hold until an agent is available to take your call. But some companies have something that's also called a callback, because it functions in the same way. Once you register with your phone number for a callback, you can hang up and carry on with your day, and the agent will call you back when they are available. In much the same way that readdir calls our phone number callback, which is described up here, or defined as the function above.
- Installing Node.js
- Writing modules and packages
- Reading and writing files
- Working with frameworks: Express, Sails, and Koa
- Generating promises
- Working with generator functions
Skill Level Beginner
Q: When I try to install Bluebird/Express/Gulp/Sails/KOA using the Node Package Manager, it seems to install correctly in the terminal. But why doesn't my project folder change?
A: You need to make sure that you've navigated to your project folder in the terminal. For example, in this course Alexander is working from the C:\Dev\ folder both in Brackets and in the terminal.