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

136
Vistas
I have an axios get call which needs to be looped 10 times and the response needs to be stored in one whole array of objects

I need to loop through the api call based on the length and then need to store the response inside the state.

The problem is that when I use Promises all it takes 1 minute 30 seconds to resolve the array of promises which I can't afford, so I need to make it synchronous so it takes around 25 seconds for one promise to be resolved and then the loop should continue until it reaches the length...

const [data, Setdata] = useState([]);

const length = 10;
let users = [];
for (i = 0; i < length; i++) {
    users.push(axios.get('/user/' + ${i}))
};

Promise.all(users)
    .then(
        response => response.map(
            res => Setdata(res.data)
        )
    );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If your goal is to only pull one user at a time, could be because you're sending over a list and the server can't handle more than one request at a time... Whatever the reason for doing one request at a time, you could do something like this.

const [data, Setdata] = useState([]);

let userIds = [1,2,3,4,5,6,7,8,9,10];
let users = [];

while (userIds.length !== 0) {
    id = userIds.pop();
    await axios.get('/user/' + ${id})
        .then(function (response) {
            users.push(response);
        });
}

response => response.map(
    res => Setdata(res.data)
)

I have not tested this, just threw it together but it might get you on the right track.

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