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

306
Vistas
How to wait for multiple nested promises to resolve?

I am attempting to make several http requests (using axios), one to each url in a list. As soon as an individual request resolves, I want to make another request to a url constructed from the previous request's response. Finally, when all secondary requests are resolved, I want to do something with all of their responses.

I have attempted to accomplish this as follows:

let promises = [];

urls.forEach(url => {
  const promise = axios.get(url);

  promise.then(response => {
    promises.push(axios.get(response.data.url))
  });
});

Promise.all(promises).then(responses => {
  // do something with responses
})

I expect the responses of all secondary url requests to be resolved when .then(responses => {}) triggers, yet when this code triggers the responses array is empty.

How can I achieve something like this?

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

0

Return the nested axios.get to a mapping callback, so that Promise.all will wait for all nested requests to complete first.

Promise.all(
  urls.map(
    url => axios.get(url).then(
      response => axios.get(response.data.url)
    )
  )
)
  .then((results) => {
    // ...
  })
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is logical considering that very first promise.then() statement is asynchronous. The urls.forEach althoughy seemingly asynchronous, it is synchronous. Thus the Promise.all() is called before any promises are pushed to your array.

You could set it up like so:

let promises = [];

urls.forEach(url => {
  promises.push(axios.get(url).then((response) => {
    return axios.get(response.data.url);
  });
});

Promise.all(promises).then(responses => {
  // do something with responses
});

This chains the .then() directly onto the first axios request, which is then pushed onto the promises array.

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