Skip to content Skip to sidebar Skip to footer

Array Returns Length Of 0 Although It Has Values

so i am trying to push items to an array and it appears to be return a length of 0 although there are items in the array. let calendarDates = [] async function getDates() { c

Solution 1:

This will help

asyncfunctionMain() {
  let calendarDates = []
  calendarDates = awaitgetDates();
  createCalendar(date, side)
}

asyncfunctiongetDates() {
  const response = awaitfetch('/calendars/fetch_dates')
  returnawait response.json();
}

functioncreateCalendar(date, side) {
  console.log('createCalendar', calendarDates, "is array?", Array.isArray(calendarDates), 'length', calendarDates.length);
}


Main();

Solution 2:

getDates is async function. So, if you call:

getDates()
createCalendar(date, side)

createCalendar may be called before getDates() do successful. Async, Promise are really important, you should practice and study carefully about them.

Post a Comment for "Array Returns Length Of 0 Although It Has Values"