Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 13, 2022 04:19 am GMT

async and await in javascript

ES2017 introduces two new keywordsasync and awaitthat 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 dont 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 weve 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)]);

Original Link: https://dev.to/digomic/async-and-await-in-javascript-2bc4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To