Fetch API error test
by cferdinandi
JavaScript
var handleResponse = function (response) {
return response.json()
.then(function (json) {
if (response.ok) {
return json;
} else {
return Promise.reject(response);
}
});
};
fetch('https://jsonplaceholder.typicode.com/postses')
.then(handleResponse)
.then(function (data) {
console.log('success', data);
})
.catch(function (error) {
console.log('error', error);
});