Node.js Lesson 14: Asynchronous Development

Node.js Lesson 14: Asynchronous Development

Node.js Lesson 14: Asynchronous Development

Hello everyone, we are going to learn about async development in this lesson. First, we will learn about what are async tasks and then learn how to handle them. Let’s start.

What is Asynchronous Operation

Asynchronous Operation refers to the flow of execution when something doesn’t follow the line by line execution of code but waits for some time. Set timeout is a general of an asynchronous task. What happens when you create a timeout task? JavaScript doesn’t wait for it to resolve, the whole flow of execution will be blocked if that happened. JavaScript continues to the next line and sends the timeout call-in event loop. Once the timeout is resolved, it runs that function. Here, two different are happening without blocking resources for the other one.

Let’s look at another example: API requests. When you call an API, it doesn’t resolve instantly. It takes a little time for it to resolve and JavaScript can continue doing other tasks while the request is in progress. This asynchronous behavior of JavaScript is what makes it so powerful. Now one thing to note here is, the JavaScript won’t end the execution of code until the requests resolve either in success or failure. JavaScript will wait for it to resolve even if there is nothing to execute further, at least for a specific time of waiting. Let’s talk some more about this with the help of callbacks.

Callbacks in JavaScript

We can pass a function to any asynchronous function and call it when the request is complete. That passed will be called as callback in javascript. It means that I will call you back later. Let’s see how:

Here, the setTimeout function takes two arguments: a callback and a timeout interval. The function passed in the first arguments serves as a callback as it will only be called after the delay. We can use such usage of the callback function to handle asynchronous tasks. There are other ways of handling asynchronous tasks. Let’s look at promises.

Promises in JavaScript

JavaScript deals with asynchronous tasks with the help of promises. What is a promise?
You can understand it as the literal meaning of the term promise. When we say we created a promise, we mean that this function promises us to return some data after some time. Promises will either resolve in success or failure but we can use it to add instructions about what happens when then promise results in success and failure. Let’s look at the code.

The above code is how you create a promise in JavaScript. It takes a function with two arguments: resolve and reject. We use resolve to end the promise if the request was successful and reject if the request has failed for some reason. Now how to use it?

Promises can be chained with .then() call which will only be called if the promise was successfully resolved. Similarly, we can chain it with a .catch() call which will only be called the promise is failed. We can use these to handle success and failure.

There is one other way to use promises and that is async/await syntax. It’s just a sugar coat over promises and makes it easy to use as well as provide more readability.

The function must be marked with the async keyword to use await syntax inside it. Code marked with await syntax will stop the normal flow of javascript execution and wait for the promise to resolve instead. We wrap the call with a trycatch block to handle the case of failure. Let’s talk about the usage of promises in the Nodejs environment.

Handling Promises in Nodejs

In any real-life scenario, it hardly ever happens that we get a response immediately. That means dealing with a lot of asynchronous tasks in a production app. Let’s consider this example where we try to read file content:

Here, we are calling readFileSync which reads the file synchronously without any delay. Imagine what will happen if the file is present in another location and it gets a little delay to read the file. This won’t work. We will have to use callback instead.

We changed the readFileSync method to readFile, it’s no longer a synchronous operation. We passed another function after the filename. This function will run after the read file operation is complete. This callback function receives two arguments: error and data. We can use these two to handle both success and failure cases.

Usually using callbacks is not so great because you might stumble upon a problem called Callback Hell. Callback Hell is a situation where you keep adding callbacks to other callbacks to handle a series of asynchronous tasks. For this reason, it is preferred to use promises instead.

Now the function readFile is a callback-based function and doesn’t support promises so you will have to convert it into a promise first. We can use our good old util module for this purpose. Let’s see how:

We used util. promisify() to convert fs.readFile into a promise and then used the .then chaining method over it. When you have to deal with multiple async tasks, you can keep adding .then one after another and avoid the callback hell problem.

Now when we are using promises, we can also use async/await syntax to make our code a little more readable.

It looks much better and easy to read, don’t you agree? So today we learned what is an asynchronous operation. How to handle them using callbacks and promises. We also saw how to use async/await syntax. We learned why using callbacks is not the best option because of callback hell. We looked at a real-life example of how to read files asynchronously with callback, promise and async/await. That’s all for today. I hope this lesson was helpful to resolve all your doubts regarding asynchronous operation.

You can find this lesson coding in our repository.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Node.js Lesson 13: Debugging in Node.js

Since debugging is this important, it's almost a skill that we must learn to be a better programmer and ship features quickly. In this lesson, we ...

Node.js Lesson 12: HTTP Module and Nodemon

Hey everyone, today we will dive deeper into the HTTP module and learn it's functionality. We will learn about what are the function provided that ...

Node.js Lesson 11: Echo Server

Today we are going to learn about Echo Servers. We will go through what are they and how to build them. We will also use this little project to ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy