I have an issue where I need to wait for a foreach loop to return each time. I tried placing await
in some places and tried to use a Promise but I can't get it working. Have a look at the below code to understand my question better.
arr = [1, 2, 3];
MyFunction = async function() {
arr.forEach(async element => {
setTimeout(() => {
console.log(element);
}, 1000);
});
console.log('done');
}
MyFunction();
With this code you get:
done
1
2
3
How do I use await
or something to make it come out:
1
2
3
done