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

125
Vistas
Sending multiple POST Req in a forEach loop React

In the code below I put a post REQ function inside a forEach loop and send a req for every selectedService entry that exists within that array. Then when the loop finished I fetch all the userServices and set that to my state.

  const handleAddServiceToUser = async () => {
    if (selectedServices === []) {
      setErr('Please Select A Service')
    } else {

      selectedServices.forEach(async (selectedService) => {
        await addNewUserService(userId, selectedService.id)
      })
      const res = await getUserServicesList(userId)
      setUserServices(res.data)

      handleClose()
      if (err !== '') {
        setErr('')
      }
      setSelectedServices([])
    }
  }

I have never seen or tried anything like this before (async forEach). The code does seem to be working when I test it in my client. I am assuming it is probably not a best practice to do something like this? Is this acceptable in terms of performance? And if not what is an alternative?

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