Async & Await: A feature You Should use carefully

Saurav Aggarwal
3 min readSep 29, 2020

Why And What is Async-Await?

if you have used javascript there’s no way you never got stuck with synchronous API calls and just that may have doubled or even tripled the time of your development. And you missed all the asynchronous programming languages that made this so easy and I understand that making an API call in js is a pain especially when you are new to it. I remember when I tried to make an API call in the first application and I just got stuck and that just irritated me. My first intent was to go back to the old behavior that I was used to that is running code sequentially and I found async-await which does exactly what I was looking for. I used that and boom my problem was solved. So if I put it in an easier way it will execute your code in sequence what I mean to say that it will wait until the API call is completed and really it was damn easy I just have to write 2 additional words.

//This won't work
function getData(){
let x = fetch("API_URL")
.then(resp => res.json())
.then(resp => resp)
return x;
}
//This will work
async function getData(){
let x = await fetch("API_URL")
.then(resp => res.json())
.then(resp => resp)
return x;
}

You can see in the above example just adding 2 words you can solve the problem isn’t it great.

Should You Use Async-Await?

Okay, this is a bit of a controversial topic to discuss. So we’ll divide this into points. Each point will cover some problem which it solves or creates.

  1. Code Understandability: Okay so by using async-await you will make your code more easily and readable because as programmers we are in a habit of seeing statement execution in a sequence and it will be much easy for a non-js developer to understand as it will be easy for him as it will be like any other programming language.
  2. Promise Dilemma: Okay this is another thing that most of the js dev’s hate and I literally can’t blame them it’s Literally something and yes I am talking about the Promise Dilemma and it is what the name tells it’s the dilemma of working with the promises or not. If you tell me I would choose not to and literally, async-await give me the power to do this because promises are something that is hard to understand and difficult to accommodate in code at least for me.
  3. Taking a Step Back: This statement is a little confusing but just stay with me I’ll try to explain it by giving you an example. JS is designed for non-blocking tasks for unnecessary things but when we use async-await we make JS wait for something as redundant it makes sense if it’s solving something but if we doing it only because we don’t want to deal promises is something I don’t think is good. I feel we are taking a step back.

Above are some things I feel about async-await but these are my own views and I would love to get more perspective regarding this.

So that’s enough theory let’s code and better understand the problem.

A minor Example

In the above example, you can see how async-await can be helpful and also the unnecessary wait because it blocked our function.

I hope the above things may have cleared some doubts and if you have some doubts feel free to connect with me I will be more than happy to help. Below is my LinkedIn profile if you want to connect and have a discussion.

--

--

Saurav Aggarwal

Just a curious dev who writes when gets bored of work life