While delving into the realm of promises, I decided to test it out with a basic GET request on Twitch. Yet, one aspect is still puzzling me - why does json()
return a promise? The response already contains the data, so what's the deal with it being wrapped in another promise?
fetch('https://api.twitch.tv/kraken/games/top?limit=10&offset=0')
.then( resp => {
resp.json()
.then(function(data) {
console.log(data);
});
});
To put it differently: I grasp that the first then
statement waits for the response. However, once inside the function of that then block, shouldn't the data be readily accessible without an additional promise since the response has already been received? This whole situation just leaves me scratching my head.