2 ms·
Your example is bad. Fetch resolves the promise even if you get a 400 or 500 error. If your project is going to be robust, you MUST intercept the result and ch
by hajile 4y ago
Your example is bad. Fetch resolves the promise even if you get a 400 or 500 error.
If your project is going to be robust, you MUST intercept the result and check before asking for JSON.
const resp = fetch(`https://api.weather.gov/gridpoints/TOP/31,80/forecast`)
if (resp.ok) {
const weather = await resp.json()
|> x => x.properties.periods[0]
} else {
...
}
Your hack-syntax response would then become
const resp = fetch(`https://api.weather.gov/gridpoints/TOP/31,80/forecast`)
if (resp.ok) {
const weather = await resp.json()
|> %.properties.periods[0]
} else {
...
}
Hardly a big win and not at all a win when you realize that you can't copy/past code to and from that syntax without risking weird errors due to the special symbol.
`await` isn't a function and (as I noted) either wouldn't be possible or would require special syntactic consideration for F# syntax, but the hack syntax is basically one giant ball of special syntactic considerations.
I'd rather explicit async/await and keep the simplicity of the F# syntax.