DEV Community

diego michel
diego michel

Posted on

async and await in javascript

ES2017 introduces two new keywords—async and await—that represent a paradigm shift in asynchronous JavaScript programming. These new keywords dramatically simplify the use of Promises and allow us to write Promise-based, asynchronous code that looks like synchronous code that blocks while waiting for network responses or other asynchronous events.

await Expressions

The await keyword takes a Promise and turns it back into a return value or a thrown exception. Given a Promise object p, the expression await p waits until p settles. If p fulfills, then the value of await p is the fulfillment value of p. On the other hand, if p is rejected, then the await p expression throws the rejection value of p. We don’t usually use await with a variable that holds a Promise; instead, we use it before the invocation of a function that returns a Promise.

let response = await fetch("/api/user/profile");
let profile = await response.json();

Enter fullscreen mode Exit fullscreen mode

async Functions

Because any code that uses await is asynchronous, there is one critical rule: you can only use the await keyword within functions that have been declared with the async keyword.

async function getHighScore() {
    let response = await fetch("/api/user/profile");
    let profile = await response.json();
    return profile.highScore;
}

Enter fullscreen mode Exit fullscreen mode

Declaring a function async means that the return value of the function will be a Promise even if no Promise-related code appears in the body of the function. If an async function appears to return normally, then the Promise object that is the real return value of the function will resolve to that apparent return value. And if an async function appears to throw an exception, then the Promise object that it returns will be rejected with that exception.

The getHighScore() function is declared async, so it returns a Promise. And because it returns a Promise, we can use the await keyword with it.

displayHighScore(await getHighScore());

Enter fullscreen mode Exit fullscreen mode

Awaiting Multiple Promises

Suppose that we’ve written our getJSON() function using async.

async function getJSON(url) {
    let response = await fetch(url);
    let body = await response.json();
    return body;
}

Enter fullscreen mode Exit fullscreen mode

And now suppose that we want to fetch two JSON values with this function.

let value1 = await getJSON(url1);
let value2 = await getJSON(url2);

Enter fullscreen mode Exit fullscreen mode

The problem with this code is that it is unnecessarily sequential: the fetch of the second URL will not begin until the first fetch is complete. If the second URL does not depend on the value obtained from the first URL, then we should probably try to fetch the two values at the same time. This is a case where the Promise-based nature of async functions shows. In order to await a set of concurrently executing async functions, we use Promise.all() just as we would if working with Promises directly

let [value1, value2] = await Promise.all([getJSON(url1), getJSON(url2)]);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)