1

demo.json content ["2018-06-01",2018-07-01","2018-08-01", "2018-09-01","2018-10-01", "2018-11-01"]

i am trying to parse a local json file and populate an existing array but unable to access the populated array outside the async function

arr1=[];
jsonurl = 'demo.json';

async function fetchdata() {
    res = await fetch(jsonurl);
    arr1= (await res.json());
    console.log(arr1); //<== this returns a populated array
}
fetchdata();

console.log(arr1);//<== this returns a empty array
ren
  • 25
  • 5
  • 1
    Hi There! This is a classic async misunderstanding of which there are thousands every day on Stack Overflow. _An async method completes after the console log has been called due to it taking time_. I don't wanna look up which questions have asked this before, but have a look into how async works and what it's for. – somethinghere Sep 01 '22 at 11:00
  • Please properly declare your variables with `const` or `let` - at the very minimum `var`. – Andy Sep 01 '22 at 11:06

0 Answers0