【发布时间】:2021-05-01 01:29:17
【问题描述】:
我一直在尝试找出惯用的 F# 方式来异步下载网页并处理任何错误/HTTP 失败代码,我认为这是迄今为止我最接近的尝试,但我在 Choice1Of2 行上遇到类型错误
我愿意
a) 了解失败的原因(我仍在学习 F#)
b)知道这是否是正确的方法/如何使这项工作发挥作用,或者我是否完全走错了路
FS0001 This expression was expected to have type 'Async<Choice<string,exn>>' but here has type 'Choice<'a,'b>'
let fetchAsync url = async {
return! Async.Catch(async {
let! str = Http.AsyncRequestString(url)
return str })
}
let result = fetchAsync "http://www.example.bad"
match result with
| Choice1Of2 v -> logger.LogInformation("worked")
| Choice2Of2 ex -> logger.LogInformation("failed")
【问题讨论】: