Await for the Async

Ian Wright
3 min readFeb 22, 2021

Since becoming a full stack engineer, the async function has yet to make an appearance in my code. The importance of the function is huge to potential employers and clients. Time complexity is a major key in the tech industry, it could arguably be the most important feature. Since its introduction in the ES7 update, it has dominated the JavaScript community.

Async vs Promises

Using promises have saved me and other developers from the feared “callback hell”, but a huge debate still remains. Async vs promises has been the talking point for a while now. Here, I can list a few examples of each feature: A little help for logical references is to think as a promise in javascript is similar to a promise in real life. When using promises always use .catch and .then methods. With promises you can add multiple handlers to a single promise. After the code runs, the promise will return either the code that ran how you wished or it will return the catch and show you the error. Now, the async function returns a promise wether you have a promise at the end or not. Async function will primarily help with blockers and other functions that are trying to run with each other.

Features of Async

The syntax for an async function is using the async keyword before the function keyword. If you are using es6 it will be placed after the equal sign. Using await can be tricky without knowing this…it can only be used in a async function. Await waits for the async function to either resolve or reject the code. Await blocks the execution of code that’s in the async function. One thing to remember is when you use await, you are writing blocking code. Async functions can have zero or many await expressions.

Writing async functions

Good practice in writing these functions are error handling. Similar to promises best practice is always catching your errors. With async functions, the way to write it is to use the try and catch keywords. If you wish you can also use the catch keyword to catch any errors. When writing an async function there is no need to return a promise, it’s already done for you as I said above. Error handling can also be a huge boost in an interview when trying to receive job offers, it’s always good to display best practices.

Overview

At some point using async can be a huge advantage, some say it’s the new way to write promises(up to you to decide). A pro is its increase in your codes performance and responsiveness of your app. Also, a benefit is the way async functions can help your code look cleaner, which is always good. After learning more about async function, I recommend trying them out. Getting practice can help your performance and impress some employers and friends!

--

--