ts await foreach loop

Certainly! Here is a response:

async function processArray(array) {
  for (const item of array) {
    await delayedLog(item);
  }
}

function delayedLog(item) {
  return new Promise(resolve => setTimeout(() => {
    console.log(item);
    resolve();
  }, 1000));
}

const myArray = [1, 2, 3, 4, 5];

processArray(myArray);

Explanation:

  1. Define an async function called processArray that takes an array as a parameter.
  2. Use a for...of loop to iterate through each item in the array.
  3. Use the await keyword to pause the execution of the async function until the delayedLog function is resolved for the current item.
  4. Define a delayedLog function that takes an item as a parameter and returns a Promise.
  5. Within the delayedLog function, create a Promise that resolves after a 1000ms timeout, during which it logs the item to the console.
  6. Create an array called myArray with some sample values.
  7. Call the processArray function with myArray as an argument to start processing the array items with a delay.