Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
Using Await inside of map
router.post('/createGroups',async (req)=>{
    const {leagueID,teams} = req.body
    await Promise.all(teams.map((group,rank)=>{
        group.map(async(t,index)=>{
            if(index === 0){
                console.log(index,rank,t)
                const createdGroup = new Group({league:leagueID, group:{team:t}, rank:rank+1})
                return await createdGroup.save()
            }else{
                console.log(index,rank,t)
                return await Group.findOneAndUpdate(
                    {league:req.body.leagueID, rank:rank+1},
                    {$push:{
                        group:{
                            $each:[{team:t}]
                        }
                    }
                })
            }
        })
    }))
})

I am trying to loop over group, take the 1st element, insert a document, wait until the document is inserted, then update the same document to add the remaining elements into the nested array (group).

Basically, I want it to wait until each promise is resolved before proceeding with next item.

I am not sure about my approach so suggestions for how I can achieve this are welcome?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda