0

I think I am having a timing issue here with how I am using async/await. I want to loop through the foo array and make an api call and after those have all run, I want to return the last subscribeToService from the forEach. I know it gets overridden each time but that is fine.

async function myFunction() {

   let foo = await makeFirstCall();

   foo.forEach(x => {
    if (!x.isEnabled) {
      const { subscribeToService } = await api.subscribeEndpoiint(
        x.id,
        x.name,
        JSON.stringify(x.name.toJSON()),
      );
    }
  });

  return subscribeToService;
}
  • From my experience, you can't rely on `forEach` when you need to `await` code inside. You should fall back to more traditional loop `for(i=0; i https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – daddygames Apr 19 '21 at 19:15
  • forEach doesn't wait for the await aka promises, use ```for``` loop – d_bhatnagar Apr 19 '21 at 19:20

0 Answers0