3 ms·
Consider the example code here in Hack, const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast` |> await fetch(%) |> await %.json()
by maxloh 4y ago
Consider the example code here in Hack,
const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast`
|> await fetch(%)
|> await %.json()
|> %.properties.periods[0]
In F# it would be much more verbose,
const weather = `https://api.weather.gov/gridpoints/TOP/31,80/forecast`
|> fetch
|> await
|> json
|> await
|> x => x.properties.periods[0]
// or |> { properties : { periods: [result] } } => result
Hack will also requires less key strikes for calling functions that have multiple augments.
- hajile 4y agoYour 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.