Fetch API error test
by cferdinandi
JavaScript
fetch('https://jsonplaceholder.typicode.com/posts')
.then(function (response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
})
.then(function (data) {
console.log('success', data);
return fetch('https://jsonplaceholder.typicode.com/posts/' + data[0].id);
})
.then(function (response) {
return response.json();
})
.then(function (post) {
console.log('success', post);
})
.catch(function (error) {
console.log('error', error);
});