Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 23, 2021 11:53 pm GMT

JavaScript async/await

In JavaScript, asynchronous code can be done many different ways. The most recent, and most readable, is using the async/await syntax to deal with promises.

If you've seen promises, you'll know the syntax is pretty awful. Very difficult to understand until you've actually done it several times, and even then it's easy to screw up.

The async/await syntax makes your code look synchronous, while still functioning the same way as it would with a promise.

To do it, you just mark a function as async, then await another promise. Your function automatically becomes a promise, and can be consumed with await or the old fashioned promise syntax:

async function go() {    try {        console.log(await callSomePromise(3));    } catch (ex) {        console.log(ex);    }    try {        console.log(await callSomePromise(4));    } catch (ex) {        console.log(ex);    }}// treat go() as a regular promise or you can use async/await againgo().then(() => {    console.log("go is done");});

See it in action here:


Original Link: https://dev.to/jtenos/javascript-async-await-8i0

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