Below is my code. I want to store the fetched JSON data in a variable outisde of the function scope. How can I do that?
Also, if I want to run some functions on the fetched JSON data, am I supposed to do them inside .then()
?
let jsondata;
fetch('https://api.myjson.com/bins/wa759')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
jsondata = myJson;
});
console.log("test");
console.log(jsondata);
This logs
> Hello (corresponding to `console.log("test");`)
> undefined (corresponding to `console.log(jsondata);`)
> (the entire JSON data)