Skip to content
Advertisement

async/await in function synchronous – JavaScript

i have this code :

const delay = () => new Promise((resolve) => setTimeout(resolve, 2000));

async function displayScore(score) {
  await delay();
  console.log(score);
}

async function processScores(scores) {
  forEach(scores, async (score) => {
    await displayScore(score);
  });
}

processScores([1, 2, 3, 4]);

Why am I getting the message that forEach is not defined?

Advertisement

Answer

You are getting an error forEach is not defined because there is no such function called forEach(). There is, though, a method Array.prototype.forEach():

scores.forEach(async score => {
...
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement