I'm trying to put a delay between each iteration in a loop using async await. I've got a helper sleep function:
const sleep = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
And this is correctly waiting between each loop:
for (let i = 0; i < 5; i++) {
console.log('waiting')
await sleep(1000)
}
However, this is not waiting between each loop:
[0, 1, 2, 3, 4].forEach(async () => {
console.log('waiting')
await sleep(1000)
});
How can I modify the forEach code block to behave as the regular for loop block with delays between each iteration of the for loop?