I want to make a delay of 1 second in each iteration of the loop. But when I am trying to do so, it is running all over and then waiting in the end.
What can be the possible change so that loop waits for 1 second before invoking the function.
Tried answer at Using async/await with a forEach loop but didn't work with this use case.
When you will run this code below you will see that first it invokes the function 5 times, then wait and then prints the date.
const arr = [1, 2, 3, 4, 5];
async function foo(item) {
console.log('Function invoked at index: ', item);
}
arr.forEach(async (item) => {
await foo(item);
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(new Date().toLocaleString());
});