Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda