3
在suave.io,有一個功能mapJson:使用組合子清理mapJsonAsync在Suave.io
let mapJson (f: 'a -> 'b) =
request(fun r ->
f (fromJson r.rawForm)
|> toJson
|> Successful.ok
>=> Writers.setMimeType "application/json")
有沒有辦法讓這個同樣使用組合程序的異步版本?我可以用手如下
let mapJsonAsync (f: 'a -> Async<'b>) (ctx: HttpContext) =
async {
let! x = f(fromJson ctx.request.rawForm)
let resp = Successful.ok (toJson x) >>= Writers.setMimeType "application/json"
return! resp ctx
}
寫出來,但它會更好不要有明確定義ctx
或中間值。
'mapJson'已經是異步的,我是否錯過了什麼? – ademar
@ademar我的意思是當'f'是異步的。更新的問題更清楚。 –