-- Main.shssdhakchina - 26 Jan 2022
JavaScript Async & Await
"async and await make promises easier to write"
async makes a function return a Promise
await makes a function wait for a Promise
Example
async function myFunction() {
return "Hello";
}
myFunction().then(
function(value) {myDisplayer(value);},
function(error) {myDisplayer(error);}
);
Await Syntax
The keyword await before a function makes the function wait for a promise:
let value = await promise;
The await keyword can only be used inside an async function.
async function myDisplay() {
let myPromise = new Promise(function(resolve, reject) {
resolve("I love You !!");
});
document.getElementById("demo").innerHTML = await myPromise;
}
myDisplay();