gpt4 book ai didi

f# - 寓言 Elmish 中的异步命令

转载 作者:行者123 更新时间:2023-12-05 00:56:46 25 4
gpt4 key购买 nike

我有这段代码,我正在使用 Fable Elmish 和 Fable remoting 连接到 Suave 服务器。我知道服务器由于 postman 而工作,并且此代码的​​变体确实调用了服务器

 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)
callServer let 结果中的行以 Object(...) is not a function 失败,但我不明白为什么。任何帮助,将不胜感激。

最佳答案

根据 Fable 文档 Async.RunSynchronously不支持,但我不确定这是否会导致您的问题。无论如何,您应该构建您的代码,以便您不需要阻止异步计算。如果是 Elmish,您可以使用 Cmd.ofAsync从异步中创建一个命令,该命令在完成时调度异步返回的消息。

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

希望这会有所帮助。

关于f# - 寓言 Elmish 中的异步命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49891110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com