Can anyone please help me with this code to make console.log(var1) work when var1 is being set within a function that takes a little time. Here is the basic problem to replicate:
<script>
var var1;
async function loadSuccess() {
await setTimeout(() => {
console.log("waiting 5000 ms");
}, 5000);
var1 = "success";
}
loadSuccess();
</script>
<script>
console.log(var1);
</script>
Since loadSuccess() takes an extra 5000ms to complete, console.log(var1) prints undefined because it runs before loadSuccess() is finished.
Can anyone help fix the code in this example to make it console.log(var1) return "success" in the 2nd script?