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 li…


This content originally appeared on DEV Community and was authored by diego michel

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();

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;
}

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());

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;
}

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

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

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)]);


This content originally appeared on DEV Community and was authored by diego michel


Print Share Comment Cite Upload Translate Updates
APA

diego michel | Sciencx (2022-06-13T04:19:45+00:00) async and await in javascript. Retrieved from https://www.scien.cx/2022/06/13/async-and-await-in-javascript/

MLA
" » async and await in javascript." diego michel | Sciencx - Monday June 13, 2022, https://www.scien.cx/2022/06/13/async-and-await-in-javascript/
HARVARD
diego michel | Sciencx Monday June 13, 2022 » async and await in javascript., viewed ,<https://www.scien.cx/2022/06/13/async-and-await-in-javascript/>
VANCOUVER
diego michel | Sciencx - » async and await in javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/13/async-and-await-in-javascript/
CHICAGO
" » async and await in javascript." diego michel | Sciencx - Accessed . https://www.scien.cx/2022/06/13/async-and-await-in-javascript/
IEEE
" » async and await in javascript." diego michel | Sciencx [Online]. Available: https://www.scien.cx/2022/06/13/async-and-await-in-javascript/. [Accessed: ]
rf:citation
» async and await in javascript | diego michel | Sciencx | https://www.scien.cx/2022/06/13/async-and-await-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.