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

232
Vistas
How to iterate multiple http request's and solve a promise until of end of loop?

Im have a request loop what send a image or multiple images at a promise and resolve returning "true" boolean value ¿the problem? the promise is resolved before ends of loop and the request is like "background" my question is "How to await the end of loop for to resolve the promise"? im try using async await at promise but does not working

export const sendQuestionArray = (questionArr) => {
  return new Promise( async(resolve, reject) => {
    await questionArr.map((question) => {
      const formSend = new FormData()
      formSend.append('idAsignacion', question.question_id)
      formSend.append('pregunta', question.question)
      formSend.append('respuesta', question.value ? 'CUMPLE' : 'NO_CUMPLE')
      formSend.append('imagenes', JSON.stringify(question.photo))
      formSend.append('comments', question.comments)
      globalApi.post('/api-agv/audit/set_answer_audit', formSend)
    })
    resolve(true)
  })
}

Thanks for the asks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can await till POST request ends on each iteration (for each question). Make sure your globalApi.post returns Promise<any>.

export const sendQuestionArray = async questionArr => {
    for (const question of questionArr) {
        const formSend = new FormData()
        formSend.append('idAsignacion', question.question_id)
        formSend.append('pregunta', question.question)
        formSend.append('respuesta', question.value ? 'CUMPLE' : 'NO_CUMPLE')
        formSend.append('imagenes', JSON.stringify(question.photo))
        formSend.append('comments', question.comments)
        await globalApi.post('/api-agv/audit/set_answer_audit', formSend) // If 'post' method returns Promise
    }
    return true // will happen only after all requests
}

By the way: you don't need to use Promise constructor and async/await at the same time, async method always returns promises.

If you need to wait till all requests ends, just call sendQuestionArray with await keyword:

// elsewhere in code
const result = await sendQuestionArray(questionArr)
console.log(result) // 'true', printed only after request sequence  
about 4 years ago · Juan Pablo Isaza Denunciar
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