Async/Await feature has been supported by default in Node.js since v7.6.0 release. So, if you are using Node.js version above v7.6.0, then I highly recommend you to start implementing async/await feature in your code.

Async/await provides two functions: async() and await().

Syntax async:

Here:

  • funcName is an optional function name. If you specify a function name, then it is known as named function expression. If you omit a function name, then it is called anonymous function expression.
  • params can be any number of arguments which can be passed to the function.

To create an async function, we need to append the async keyword at the start of the function definition. That’s it. It’s super easy. For example:

Async functions always returns promises, no matter the type of value being returned within the function. So, to really understand the Async/Await feature, the basic understanding of Promise and how it works is a must have knowledge.

Now that a function has been defined using the async keyword, we can use await keyword inside of that function. Note that await can only be used inside of an async function. If await keyword is used outside of an async function’s body, then it will throw a SyntaxError.

Syntax await:

Note:

Await always takes a single argument which can either be a promise object or any type of value.

Here:

  • expression can be a promise or any type of value.
  • If a promise is passed to an await expression, then the await keyword waits for the promise to be settled. The final result can either be a fulfilled value or a rejected reason. If it’s a fulfilled value, it simply returns the value. If it’s a rejected reason, then it throws an error which we will need to handle properly.
  • If any type of value other than promise is passed to an await expression (i.e. if the value is not a promise), then that value is converted to a resolved promise

  • value is a variable containing the resolved value of a promise.

For example:

 

As you can see from the above code snippet that, await can be used to wait for the async function expression or promise to be resolved. The await expression pauses the async function code execution until a Promise has been settled in a non-blocking way. Since a promise is an object which represents the eventual result of an asynchronous operation, the result can only be either a resolved value or a reason explaining why it’s rejected (usually an error object). Once the promise settles either with a resolved value or a rejected reason, it will resume the async function’s code execution. If the Promise is rejected, the await expression throws an error with the rejected reason.

There are several benefits of using async/await feature in our Node.js code.

1.Clean and concise code

The biggest benefit of using async/await feature is that it will make asynchronous code look and feel like synchronous code which most of us are comfortable with. There is no need to write callback functions nor we need any thenables. This avoids nesting code into too many levels. With the clean and short code, there is much lesser probability of error margin than those compared to using callbacks and promises.]]

For example:

Promise version:

Async/Await version:

 

Even though the code may seem and feel like synchronous, pausing the code execution within the async function’s body on each await, it doesn’t block the Node’s event loop. Node’s event loop can continue executing other processes and code whilst those operations are pending.

2.Better error handling mechanism

 it’s now so simple to handle the exceptions that most of the time you just need to wrap your code into try/catch block inside of the async function’s body. Yeah, you heard it right. It is finally possible to handle both asynchronous and synchronous errors with try/catch block when used with async/await.

Let’s look at a simple error handling mechanism and compare the code between promise and async/await.

Promise version:

First of all, you might have noticed that we have used two catch blocks to handle the exceptions. This is because the outer catch block or the parent catch block will only handle exceptions thrown by synchronous code. In this case, any error thrown by JSON.parse() statement and any other synchronous code statements if present will be handled by it. However, it can’t handle errors thrown by the asynchronous code since by the time asynchronous code is executed, the outer catch statement is already executed.

Async/Await version:

As you can see that, the code is so clean and clear.

3.Eliminates callback hell

Since there is no need for callbacks or enables with async/await, no more worries about callback hell.

4.Simpler Debugging

Debugging code written using async/await is much easier than to debug the code written using either promises or callbacks.

Thanks….