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

235
Views
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 answers
Answer question

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 Report
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!