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

221
Views
¿Cómo puedo esperar para enviar una respuesta rápida hasta que finalice el ciclo asíncrono?

Querer hacer algo como a continuación. Supongo que es un problema con la llamada asíncrona ya que la respuesta que envío siempre es una matriz vacía, pero la API está devolviendo datos. Bastante nuevo en esto, ¡cualquier aporte es muy apreciado!

 app.get('/:id/starships', (req, res) => { let person = findPersonById(people, req.params.id)[0]; let starshipUrls = person.starships; for(let i=0; i<starshipUrls.length; i++){ axios.get(starshipUrls[i]).then(response => { starships.push(response.data); }) .catch(err => console.log(err)); } res.json(starships); })
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

axios.get devuelve una promesa . Use Promise.all para esperar varias promesas:

 app.get('/:id/starships', (req, res) => { let person = findPersonById(people, req.params.id)[0]; Promise.all(person.starships.map(url => axios.get(url))) .then(responses => res.json(responses.map(r => r.data))) .catch(err => console.log(err)); })
about 4 years ago · Santiago Trujillo 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!