使用组合器清理Suave.io中的mapJsonAsync

时间:2016-01-27 09:03:34

标签: f# suave

在suave.io中,有一个函数mapJson

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或中间值。

1 个答案:

答案 0 :(得分:0)

我没有看到任何方法。最后一个表达式可以简化一点。

let mapJsonAsync (f: 'a -> Async<'b>) =
  fun (ctx : HttpContext) ->
    async{
      let! result = f (fromJson ctx.request.rawForm)
      return Successful.ok (toJson result ) ctx >>= Writers.setMimeType "application/json"
    }
相关问题