Silly async example
To show that all of the code that comes after the "await" is conceptually as if it was written inside the "then()"
by Deepak Anand
HTML
<button id ="bt">Click</button>
JavaScript
console.log("sync start")
document.addEventListener("DOMContentLoaded", async function(event) {
// Set timeout
// const promiseSetTimeout = (ms) => new Promise((resolve) => window.setTimeout(resolve, ms));
// usage
// console.info('waiting on timer to finish');
// await promiseSetTimeout(6000);
// console.info('timer completed');
//
function waitForClick(element, message) {
return new Promise(function(resolve) {
element.addEventListener(
"click",
function(e) {
resolve(e.target.textContent + message);
}
);
});
}
var btn = document.getElementById("bt");
const e = waitForClick(btn, " button");
// To show that all of the code that comes after the "await"
// is conceptually as if it was written inside the "then()"
console.log(e);
console.log("sync done: executed only after the button is clicked");
});