Re:VS Code can do that?
by scottmf on Saturday April 21, 2018 @12:26PM
(#16892016)
Slightly off topic but this code in example #5:
Why not just:
[ Parent
| on HN ]
Re:VS Code can do that?
by AaronFriel on Saturday April 21, 2018 @01:35PM
(#16892338)
That has fewer brackets to colorize.
By the end of a day, he reports number of lines written and committed.
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-liter...
Re:VS Code can do that?
by azerothian on Saturday April 21, 2018 @06:51PM
(#16893798)
or
async get() {
const response = await fetch(`${baseAPI}/heroes`);
return response.json();
}
Re:VS Code can do that?
by OutThisLife on Sunday April 22, 2018 @01:03AM
(#16894792)
or
async get () {
return await (await fetch(`${baseAPI}/heroes`)).json()
}If you see `return await` glued together just like that you can drop the `await` as it's effectively a no-op in this case.
Re:VS Code can do that?
by daybreaker2 on Saturday April 21, 2018 @02:41PM
(#16892667)
Because he needs it wrapped in a promise, sending a resolve/reject?
Fetch and response.json() both return promises. So it's still a promise.
A promise is a promise ...
Re:VS Code can do that?
by Guillaume86 on Saturday April 21, 2018 @04:57PM
(#16893397)
True and in this particular example for response.json() it doesn't even matter.