I was looking for this stuff on the internet and I found that: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch. I am relativey new to typescript so there are a few things that confuse me.
First what's the difference between:
fetch('data.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
//data = myJson;
});
and
fetch('data.json')
.then(function(response) {
console.log(JSON.stringify(response.json()));
//data = response.json();
})
The first works well but the second piece of code does't. Why is that? As far as I know fetch retuns the promise of JSON and in .then, response.json() holds the actual JSON object. But the second version doesn't work (I get undefined in console).
My second question is how to get out the value of myJson. I tried doing that with global variable but it didn't work (I get undefined in console):
var data
fetch('data.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
//console.log(JSON.stringify(myJson));
data = myJson;
});
console.log(JSON.stringify(data));