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
async function Main() {
let calendarDates = []
calendarDates = await getDates();
createCalendar(date, side)
}
async function getDates() {
const response = await fetch('/calendars/fetch_dates')
return await response.json();
}
function createCalendar(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"