Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!