Asynchronous JavaScript :
Table of contents
Almost every software engineer has used async/await before, but only a handful know how it works :)
To begin with, it's essential to identify how async/await works with our code. The common assumption is that async/await only uses for doing code asynchronously there are certain points to know about its different states & the execution process. as we have certain ways to do asynchronous coding but async/await gives better performance than them.
For more information read the below blog -
💡 Async: The async keyword is used to define a function as an asynchronous function. it means the function will return a promise - which can be resolved or rejected depending on the outcome of any asynchronous operations inside the function.
💡 Await: The await keyword is used inside an async function to pause the execution of the function until a promise is resolved or rejected. The value of the resolved promise is then returned from the await expression.
💡 How it works?
When an async function encounters an await expression, it automatically wraps the function in promise. This promise will be resolved with the value returned by the await or rejected with an error if the promise is rejected.
💡 Syntax :
function fetch data = async () => {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
}
catch (error) {
console.error(error);
}
}
💡 Here's an example of how async/await can be used to simplify asynchronous code...
🎯 In Short: It allows a program to run a function without freezing the entire program. Async/Await makes it easier to write promises. The keyword ‘async’ before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.
🎯 In-depth: As JavaScript is a synchronous language, async/ await is a feature, which makes it easier to work with asynchronous code. such as - network call, file I/O(Input/ Output) and other time consuming operations.
async/await allows us to write a code that looks and behaves like synchronous code, without blocking the main thread of execution.
async/await is based on promises which is the best way of error handling for asynchronous operations in JavaScript.
async/await is essentially a syntax sugar for working with promises, making it easier to write & reason about asynchronous code.
💡 Pros :
Simplifies asynchronous code: async/await makes it easier to write & reason about asynchronous code by allowing it to look and behave like synchronous code. It makes it easier to understand and debug the code.
Better error handling: async/await allows for better error handling by using try/catch blocks to capture and handle errors effectively.
Avoids callback hell: async/await helps in avoiding "Callback hell ", a common problem in asynchronous code where multiple nested callbacks can become difficult to manage and read.
Better readability: The syntax of async/await can make code more readable(self-explanatory)and easier to understand than other methods of handling asynchronous operations, such as Promises or callbacks.
💡 Cons :
Require modern browser support: async/await requires a modern version of JavaScript & may not work in older browsers. which could limit the audience for our application & website.
Not always faster: while async/await simplifies asynchronous code, it may not always be faster than other methods of handling asynchronous operations such as promise and callback.
Can lead to increased memory usage: The use of async/await can lead to increased memory uses, as it may require additional objects to be created & stored in memory.
Can lead to confusion: If used improperly, async/await can lead to confusion & unexpected behaviour, such as infinite loop or unintended blocking of the main thread of execution.
Overall, async/await can be a powerful tool for simplifying asynchronous code and improving the readability, and maintainability and helps reduce the likelihood of errors caused by callback functions or promises chaining of JavaScript projects. However, it is important to consider its limitations and potential drawbacks before deciding whether to use them in a project.
Subscribe to my newsletter
Read articles from Aparna Mehta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by