by scottmf · 8y ago · 0 comments
get() {
return new Promise((resolve, reject) => {
fetch(`${baseAPI}/heroes`)
.then(response => response.json())
.then(json => resolve(json))
.catch(err => {
reject (err);
});
});
}
Why not just: get() {
return fetch(`${baseAPI}/heroes`)
.then(response => response.json());
} 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-liter... async get() {
const response = await fetch(`${baseAPI}/heroes`);
return response.json();
} async get () {
return await (await fetch(`${baseAPI}/heroes`)).json()
}