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

277
Visualizações
Why I'm getting troubles with this axios call?

I want to get 10 calls, each call to this endpoint return me a single item so i used axios in a for loop.

In the axios call I push in the empty array the response.data, but for timing issues (I think) if I console.log(wordsArray) outside the for loop it consoles a "strange" apparently empty array that can be opened and it has objects inside, but they don't seem to be part of the array.

Also if I try to console.log(wordsArray[0]) outside the for loop I get undefined.

What's wrong with the code??

let wordsNumber = 10;

let wordsArray = [];


for(i = 0; i < wordsNumber; i++) {
    axios.get('https://flynn.boolean.careers/exercises/api/random/word')
        .then( res => {
            wordsArray.push( res.data )
            
            // If I log the wordsArray here, inside the for loop, I get what I need.
            // But obviously I need this to be outside of the loop.
            // console.log( wordsArray ) 
            
        })
        .catch( err => {
            console.log( err );
        });
}


// logs an apparently empty array that can be opened, it has 10 objects inside
console.log( wordsArray )

// is undefined
console.log( wordsArray[0] )

This is the HTML, just for showing you the axios CDN

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="script.js" defer></script>
    <title>Project</title>
</head>
<body>
  
</body>
</html>
about 4 years ago · Juan Pablo Isaza
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