DEV Community

Cover image for Async/Await, When, Why, How, Where, Advantages/Disadvantages, and all…
Kritika Gaur
Kritika Gaur

Posted on

Async/Await, When, Why, How, Where, Advantages/Disadvantages, and all…

Image description

Hello Greeting all,

Here we are going to talk about Async/await. We will dive deep into it.
so here are some points which will be discussed here…

What is Async/await,
When it comes,
Why it come,
Where we are using it,
How it is working,
How we can use it,
Advantages and Disadvantages.

Q1. What is Async/await 🤔?

It is a concept that comes into play to handle asynchronous operations like file reading and writing, getting data from an API, etc. It addresses improving the readability and maintainability of our code. That makes our code look synchronized, which can help read and debug it. It is used in some languages like JavaScript, Python, and C#.

Q2. When it comes?

It was added to the support list in 2017 as part of ECMAScript 2017.

Q3. Why did it come?

It comes to handling Asynchronous operations to perform non-blocking operations without blocking other operations which takes time to execute. This is securing our code to issues that arose from nesting call-back (Call-back-Hell or pyramid of doom), like:- HTTP requests, fetching data from APIs, Network requests, File I/O, etc.

Q4. Where we are using it?

It is mainly used in places where you need to perform asynchronous operations: -
Web Development:- Fetching data from API, handling HTTP requests.
-File I/O:- Reading or writing Files
-Databases:- Databases queries for performing all queries asynchronously.
-Concurrency:- Managing concurrent tasks and ensuring non-blocking code execution.

Q5. How does it work?

As I mentioned. It performs non-blocking operations. There are two keywords that define their roles in performing it. What are the roles they are performing? Let’s talk about them:
async:- We are using it to define a function as asynchronous. It is returning a promise object. Promises are objects that represent the output as a response and reject an asynchronous operation.
await:- This can be used inside an async function. Which is holding the promise until it gets resolved. It is waiting for the promise to be fulfilled.

Q6. How we can use it?

While creating a function, we just need to add an async keyword before the function name and add await before the request to which it is responding.
See the example in JavaScript below:

async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error:', error);
  }
}
Enter fullscreen mode Exit fullscreen mode

Q7. Advantages and Disadvantages:-

Advantages:-

  1. Readability:- It makes asynchronous code look synchronous which is easy to read and debug it.
  2. Error Handling:- It simplifies error handling using try and catch which helps to handle errors easily.
  3. Sequential Flow:- We can write asynchronous code which executes sequentially.
  4. Reduced call-back hell:- It is removing call-back hell to make code more readable and maintainable.

Disadvantages:-

  1. Complexity: While async/await simplifies many aspects of asynchronous programming, it can be challenging to grasp the underlying concepts, especially for beginners.
  2. Compatibility: Not all environments support async/await out of the box, and transpilation may be required for older JavaScript environments.
  3. Overuse: Overusing async/await for every small operation can lead to performance issues, as it introduces overhead.
  4. Debugging: Debugging asynchronous code can still be tricky, especially when dealing with complex Promise chains.

I will appreciate your love and support if I make any mistake then please let me know and help me to improve.
If you liked my blog and want some more information about any topics related to the below points you can add comments and I will try to give my best in it.
-JavaScript
-Python
-Angular
-Ionic
-React
-MongoDB
-MySQL
-AWS
Thank You

NOTE:- You can also check my Blogs on Medium:- Kritika Gaur
Hit likes add comments and follow me for more
Enter fullscreen mode Exit fullscreen mode

Top comments (0)