3 ms·
For comparison, here's the first example, but using fetch: fetch('/my/url').then((r) => r.json()).then((data) => { }) And as an added bonus, fetch us
by Ajedi32 8y ago
For comparison, here's the first example, but using fetch:
fetch('/my/url').then((r) => r.json()).then((data) => {
})
And as an added bonus, fetch uses promises, so in an async function that's just:
let data = await fetch('/my/url').then((r) => r.json())
- aloisdg 8y agoYou can write `(r)` like this `r`. Remove clutter. You don't write `let i = (0)`
- klodolph 8y agoThat’s not a good analogy at all, the () is there in arrow functions always except, as a special case, it’s optional if there’s a single argument.
- Cthulhu_ 8y agoI'm not a fan of mixing await and .then myself; let data = await (await fetch('/my/url')).json(); That's a one-liner, but you can split the request and json data as well (and you usually want to anyway because of error handling).