In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.
Purpose of ASYNC/AWAIT ?
The purpose of async / await is to simplify the syntax necessary to consume promise-based APIs. The behavior of async / await is similar to combining generators and promises. Async functions always return a promise.
Async and await are built on top of promises to express asynchronous actions. Inside the function, the await keyword can be applied to any Promise, which will defer the execution until the promise resolves. Functions with the async keyword return a Promise.
Functions with the keyword async can perform asynchronous actions but still, look synchronous. The await method is used to wait for the promise to either get fulfilled or rejected. The await method blocks the execution of the async function at the place it is located in.
Async functions make the code more readable and are easier to handle than promises.
async :
The async keyword allows you to define a function that handles asynchronous operations. An async function returns a Promise, and it lets you write code with Promises in an imperative manner.
async function greet() {
return "Hello";
}
async function greet() {
return Promise.resolve('Hello');
}
Arrow Function:
let greet = async () => 'Hello';
await:
The await
keyword to wait for a Promise
to settle either in resolved or rejected state. And you can use the await
keyword only inside an async
function:
async function myFun() {
let result = await greet();
console.log(result);
}
In this above example, the await
keyword instructs the JavaScript engine to wait forthe greet()
function to complete before displaying the message.
Example:
// promise
let promise = new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('Promise resolved')}, 1000);
});
// async function
async function myFunc() {
let result = await promise;
console.log(result);
console.log('Welcome to Fullstackadda.com');
}
// calling the async function
myFunc();
Promise resolved
Welcome to Fullstackadda.com
Advantages of ASYNC/AWAIT :
Async/await is a relatively new feature in JavaScript that provides a simpler and more intuitive way to write asynchronous code. Here are some of the advantages of using async/await:
- Simplified syntax: Async/await provides a simpler and more readable syntax for asynchronous programming, by allowing you to write asynchronous code that looks and behaves like synchronous code.
- Error handling: Async/await makes it easier to handle errors in asynchronous code, by allowing you to use traditional try/catch blocks to catch and handle errors that occur during asynchronous operations.
- Avoiding callback hell: Async/await can help you avoid callback hell, a common problem in asynchronous programming where nested callbacks can make your code difficult to read and maintain.
- Sequential code execution: Async/await makes it easy to execute asynchronous operations in a sequential manner, by using the await keyword to wait for a promise to resolve before moving on to the next line of code.
- Improved debugging: Async/await can make debugging asynchronous code easier, by providing better stack traces and error messages that show the exact location of errors in your code.
- Improved code readability: Async/await can make your code more readable and easier to understand, by eliminating the need for nested callbacks and other asynchronous programming constructs. This can make it easier for other developers to understand and work with your code.
- Improved performance: Async/await can improve the performance of your application, by allowing you to execute asynchronous operations in parallel using Promise.all and other Promise methods. This can help reduce the overall execution time of your code and improve the user experience.
- Better integration with other APIs: Async/await can make it easier to work with other APIs and libraries that use Promises or async functions, by providing a consistent and familiar syntax for asynchronous programming. This can help you integrate with other APIs more quickly and with less effort.
- The async keyword allows you to define a function that handles asynchronous operations.
- The
await
keyword to wait for aPromise
to settle either in resolved or rejected state.