Using Promises with async/await
by dshilkret
HTML
<div>
This function uses async and await. async ensures the function returns a promise, and await is implied when returning values. The error handling is done with throw, and .then() or .catch() works just as it does with standard promises.
Using await to Replace .then().
</div>
JavaScript
const myAsyncFunction = async () => {
let success = true;
if (success) {
return "Promise resolved with async/await!";
} else {
throw "Promise rejected with async/await.";
}
};
myAsyncFunction()
.then(result => console.log(result))
.catch(error => console.error(error));