Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Command in fable Elmish

I have this code I'm running using Fable Elmish and Fable remoting to connect to a Suave server. I know that the server works because of postman and there are variations of this code that does call the server

 let AuthUser model : Cmd<LogInMsg> =
        let callServer = async {
                let! result = server.RequestLogIn model.Credentials
                return result
            }
        let result = callServer |> Async.RunSynchronously
        match result with
        | LogInFailed x -> Cmd.ofMsg (LogInMsg.LogInRejected x) 
        | UserLoggedIn x -> Cmd.ofMsg (LogInMsg.LogInSuccess x)

The callServer line in the let result fails with Object(...) is not a function, but I don't understand why. Any help would be appreciated.

like image 360
user1742179 Avatar asked Apr 18 '18 04:04

user1742179


1 Answers

According to Fable docs Async.RunSynchronously is not supported, though I'm not sure if that is causing your problem. Anyway you should structure your code so that you don't need to block asynchronous computations. In case of Elmish you can use Cmd.ofAsync to create a command out of an async that dispatches messages returned by the async when it completes.

let AuthUser model : Cmd<LogInMsg> =
    let ofSuccess result =
        match result with
        | LogInFailed x -> LogInMsg.LogInRejected x
        | UserLoggedIn x -> LogInMsg.LogInSuccess x
    let ofError exn = (* Message representing failed HTTP request *) 
    Cmd.ofAsync server.RequestLogIn model.Credentials ofSuccess ofError

Hopefully this helps.

like image 50
hvester Avatar answered Oct 20 '22 15:10

hvester