I'm trying to implement an HTTP GET using fetch API. I have followed the example provided here and it works however I don't understand exactly what the chain of callbacks does.
In attempt to understand better the example above, I have tried to replace the code provided in the example above with some lines of code that, in my mind, should get the same result but my code does not work and the console returns the following error: response.json is not a function
The following code works (a JSON object is shown in console):
fetch('http://192.168.1.6:8000/lending/getBook/IsbnTest2')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
})
The following code is not working (error "response.json is not a function" is shown in console):
let response = fetch('http://192.168.1.6:8000/lending/getBook/IsbnTest2');
let myJson = response.json();
console.log(JSON.stringify(myJson));
Can you help me to understand what are the differences between the two pieces of code and what the working one is actually doing?
Thanks!