0

I have a API call to update a record in a loop. I want to have a promise around API call so that the next iteration is not called until the first is finished. How can I achieve this?

I have tried this but this gives an error "Can not use keyword 'await' outside an async function"

async saveMyData(){
  this.elements.forEach(element => {
        await this.$refs.DM.saveInternalData(element)
  })
}
user2837961
  • 1,505
  • 3
  • 27
  • 67

1 Answers1

2

If you want to use an async function in a loop, you can do:

for (let i = 0; i < elements.length; i += 1) {
  await foo();
}
Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80