haskell - 是的,withAsync

标签 haskell asynchronous yesod

我是 Haskell 和 Yesod 的新手,我正在尝试使用 Control.Concurrent.Async 模块来执行异步操作。 (代码基于:https://hackage.haskell.org/package/async-2.1.1/docs/Control-Concurrent-Async.html#v:withAsync)

quizWidget = do
   --Get first question   
    withAsync (showQuizItem 1 1 ) $ \qi -> do
    withAsync (showScoreboard)    $ \sb -> do

    quizItem <- wait (qi)
    scoreboard <- wait (sb)

    toWidget $(hamletFile "hamlet/quiz.hamlet")

但这会产生以下错误:

"No instance for (MonadWidget IO) arising from a use of ‘toWidget’".

所以问题是我做错了什么?

最佳答案

异步包中的 API 是单态的,这意味着它仅适用于 IO 中的操作。您尝试在 WidgetT 转换器类型中使用它,这就是错误消息的来源。您可以使用 lifted-async包,它提供了 withAsync 函数的多态变体。在大多数情况下,这应该只是一个问题:

  1. 导入Control.Concurrent.Async.Lifted
  2. lifted-async 添加到您的 build-depends

关于haskell - 是的,withAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42503446/

相关文章:

c# - Visual Studio 在异步调用异常时中断,而不是陷入 Catch() 中

json - 从后台线程修改自动布局引擎

haskell - 如何为自定义新类型派生 PersistField?

haskell - 这让表达式如何工作?

list - haskell 递归

multithreading - 如何在Websocket中使用超时,同时又不影响应用程序安全性?

haskell - yesod - 获取 POST "Content-type: application/json"的请求正文

facebook - 如何使用 yesod-auth-fb 从 Facebook 登录获取用户的电子邮件地址?

haskell - 与类型相关的奇怪错误

math - 我应该如何在 Haskell 中实现凯莱表?