2016-01-27 41 views
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或中間值。

+0

'mapJson'已經是異步的,我是否錯過了什麼? – ademar

+0

@ademar我的意思是當'f'是異步的。更新的問題更清楚。 –

回答

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" 
    }