Promises - Fetch Method
Video
JavaScript Notes
JavaScript
let url = "http://jsonplaceholder.typicode.com/comments?postId=42";
fetch(url)
.then(response => response.json() )
.then(data => {
let str = JSON.stringify(data, null, '\t');
//output the string version of the JSON data as preformatted text
//document.querySelector('#output pre').textContent = str;
//output the postId from the first object in the data array
document.querySelector('#output').textContent = data[0].postId;
})
.catch(err => {
let nm = err.name; //Error Type
let msg = err.message; //The error message
alert(`CATCH: ${nm} ${msg}`);
});
Feedback
Submit and view feedback