-2

i've a similar situation

array.forEach(item => {
 function_fetch(item)
})

now i need that before that loop continue he await that the function end and do what have to do, then recall the function with the other item

  • 5
    Without seeing the inner function it's impossible to help you. Please modify the question to include a **[mcve]** - use the snippet button in the editor -> `<>` – Reinstate Monica Cellio Apr 30 '19 at 09:35
  • Maybe [your async function doesn't contain an `await` statement](https://stackoverflow.com/q/45594596/6320039)? – Ulysse BN Apr 30 '19 at 09:37
  • 1
    even if you have await in your foreach it must be forEach(async (item) => {}) which will return a promise and you will have to wait for the same as well. – AZ_ Apr 30 '19 at 09:39

1 Answers1

-2

You can not use await in forEach function directly.

So you can use simple for of loop.

async function fetching () {
  for ( const item of array) {
    await function_fetch(item)
  }
}

await function should be inside any async function only.

ATUL SHARMA
  • 184
  • 2
  • 12