Make Javascript sequential with "async" and "await"

By Abhineet Mittal | Coding
Updated on: April 11, 2020

Most of the older programming languages are sequential in nature. That means, when the code runs, it is executed line by line. If there is a function call, the next line is executed only when the function has been executed and returned control. This is blocking in nature and helps programmers easily predict the output of their code.

But when we move to async languages like Javascript, code is non-blocking in nature. That means, execution does not wait for any function to return response. This is good for performance, but creates a problem when the next line of code requires output from the function call made in previous line.

To solve this problem, Javascript provides Promises. Promises allow us to write a callback function to process the response of the called function. But their syntax requires to write another function to handle the response. Also, if there are multiple functions, it may lead to callback hell.

The syntax of a function returning a promise is:

function add(a, b) {
    return new Promise(function(resolve, reject) {
        resolve(a+b);
    });
}

And this is how we handle the returned Promise:

add(2,3).then(function(response) {
    console.log(response);
}

This becomes cumbersome if there are a lot of function calls nested inside one another. To simplify this code, there are two keywords in Javascript to write promises in a very simple way- async and await.

async

async can be placed before a function definition. It means that the function will always return a promise. If the returned value is not a promise, it will wrap it in a promise and return.

async function demoFunction() {
    return 1;
}

await

await can be placed before any line of code to ensure that Javascript hold the execution till that line is executed. If there is a function call in that line, Javascript is wait till the function returns a response.

async function demoFunction() {
    let sum = await add(a, b);
    console.log(sum);
}

In the above example, we are calling a function called add and saving its value in a variable called sum. The await ensures that the next line is executed only after we get the value returned from the function add. If we remove the await keyword from this, console.log may print an empty variable.

So, even if the called function is returning a promise, there is no need of defining any callback function. Just simply store its return value in a variable and process it in the next line of code. This is pretty much like we do stuff in C or PHP.

Note: await can only be used inside an async function.

This is such a simple syntax that can easily make Javascript code sequential whenever needed.

To read the detailed description of these keywords, you can refer to the following articles:

Abhineet Mittal
Abhineet is an entrepreneur by profession and programmer by heart. He loves to write about WordPress. He is the Founder of popular Digital Publishing Platform, Sahityapedia.