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

197
Visualizações
Why asynchronous function doesn't fill array properly

I am making an api call, and I understand that I need to use asynchronous functions to wait on the call results before I can work with them. However, I can't get the array to fill properly even though it is in an asynchronous function. I am sure there is something obvious that I'm missing, maybe my logic to populate the array isn't in the right place?

Here is my code:

var data;
const getEvent = async() => {
  const res = await fetch(apiCall, requestOptions)
  const res2 = res.json();
  return res2
}

data = async() => {
  const temp = await getEvent()
  for (let i = 0; i < temp._embedded.events.length; i++) {
    eventlist.push(temp._embedded.events[i]);
  }
}

data()

console.log(eventlist)
console.log(eventlist[1].name)

Obviously before this I build out the apiCall and requestOptions variables so they call the api properly.

The console comes out looking something like this. The array is partially populated but should have 10 event items, not just one. I can't access any of the elements within the array.

Console Snippet

Any help would be greatly appreciated!

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