Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
how do I use async await in a regular for loop

I have a function that accepts a game found from a mongodb database via mongoose and the number of bots to add to the game object. the problem is after running the function only one bot object is being saved to the document even thought numOfBots is 3

async function addBot(game, numOfBots){

  for(index = 0; index < numOfBots; index++){
    let bot = {nickName: "Bot"}
    game.players.push(bot)
    game = await game.save()
  }
  return game
}

// elsewhere in another async function 
await addBot(game,3)

How can I use async await correctly in a regular for loop?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!