How to use async await in node js
Node js is a cross-platform and open-source framework and provides n number of useful JavaScript modules that enhances the coding experience and makes web applications development simpler. A few key concepts that are listed in node js interview questions on our website can give you a good insight of all you need to prepare for interviews on node js. It is easy to install, learn, and practice.
What are async-await and Promise?
Async / await
In real-life applications, a lot of nested callback functions are needed. Each asynchronous call can then become a pain and lead to confusions. Using async/await simplifies the process of writing nested promises that would otherwise be confusing. It creates a non-blocking code. To make a function async, we need to add the keyword async to the function definition, so that the function returns an async object.
Example
async function sum (a, b){
return a+b;
}
Promise
To know how to use async/await, we first need to understand how to write code using promises. We have discussed with command in detail in our node js interview questions and answers section.
How to use async-await with promise?
Let us go through the steps to use async-await with a promise.
# Step 1
You have to install async with npm install async
command.
# Step 2
Call or import the async in the file where you want to use async.
var async = require("async");
# Step 3
Step3. Make an async function and call await function inside async function.
let phoneChecker = async function (req, res){
const result = await phoneExistOrNot();
}
exports.phoneChecker = phoneChecker;
Await will work only under async function.
# Step 4
Now you can write your business logic in await function.
let phoneExistOrNot = async function (req, res){
return new Promise(function(resolve, reject) {
db.query('select name, phone from users where phone = 123456789 ', function (error, result) {
if(error) {
reject(error);
console.log('Error');
} else {
resolve(result);
console.log('Success');
}
})
});
}
Why we need async-await and promise?
Though just using promise serves the purpose, imagine having more and more nesting – the code would break all the coding standards and at some point, be beyond comprehension. Our collection of advanced node js interview questions explains this point in a more detailed manner.
This is why we need async/await – to eliminate the wrong output issue as well as make our code more straightforward and more robust.
The benefit of async-await and promise
It depends on a developer whether to choose promise or async/await in their code. However, all async functions implicitly return a promise, and every function that returns a promise is an async function. Using a combination of promise and async/await, you can get the benefits of both.
- You can create multiple small async functions that can run parallelly if required. If they need to run one after the other, use await.
- The code looks more straightforward and easy to read.
- Easier to debug.
- The code with async/await behaves more like synchronous code.
- Both synchronous and asynchronous errors can be handled using try/catch.