4 ms·
agree. I would only add rejecting when !response.ok function fetchPost(uri, body) { return new Request(uri, { method: 'POST', mode: 'cors',
by rofrol 8y ago
agree. I would only add rejecting when !response.ok
function fetchPost(uri, body) {
return new Request(uri, {
method: 'POST',
mode: 'cors',
headers: {
'Authorization': 'token 123412341234'
},
body: JSON.stringify(body)
})
}
function fetchStatus(response) {
return response.ok ? Promise.resolve(response) : Promise.reject(new Error(response.statusText));
}
fetch('https://httpstat.us/500')
.then(fetchStatus)
.then(function(result) {
console.log(result)
})
.catch(function(err) {
console.log('BAD:', err)
});
https://medium.com/@luminarious/i-was-trying-out-fetch-literally-this-morning-and-i-notice-you-kind-of-skipped-a-simple-solution-62a734c28dfb https://medium.com/@luminarious/i-was-trying-out-fetch-liter...