I'm going back and forth on StackOverflow trying to find this specific answer, but I can't find anything that works. "Test" should be defined after the execution of the async function inside the loop. If I try to console log "Test" it returns undefined. How can I wait for the loop execution to proceed in the stack?
let array = [1, 2, 3, 4, 5]
let test;
array.forEach(async el => {
await setTimeout(() => {
if(el === 5) {
test = true
}
}, 5000)
})
console.log(test)
I also tried defining an async function with the loop into it and then calling an IIFE, awaiting for the function and console logging the variable. It returns the same undefined.
let array = [1, 2, 3, 4, 5]
let test;
async function hey() {
array.forEach(async el => {
await setTimeout(() => {
if(el === 5) {
test = true
}
}, 5000)
})
}
(async () => {
await hey();
console.log(test);
})()