Fetch
fetch(requestURL)
.then(function(responseData){
// Fetch will package the response into an object with some methods that allow us to do some useful things with the response.
// Use the .json() method to return the data in JSON format
return responseData.json();
})
.then(function(jsonData){
// whatever we return in the first .then promise will be passed into this callback function
// do some stuff with the jsonData here
})
.catch(function(error){
// any errors encountered in the request or the .then promises above will be passed into this callback
console.log("Oh no, there's been an error!", error);
})Example
First let's look at our data in the console
Now let's get some of this data rendering on the page!
Exercise
Last updated