[英]How convert Cats Effect 3 to Future
我正在嘗試將 Playframework 與 Cats Effect 3 Tagless Final 風格相結合。
我被困在向未來的轉變上。 Play 的 Action 需要我想要達到的值或 Future 來進行異步處理。
def method = authed { _ =>
val program: EitherT[IO, Throwable, Int] = ???
program
}
def authed[F[_]: Async](fa: Request => F[Result]): Action = {
???
}
在貓效應 2 中,可以通過_.toIO.unsafeToFuture
實現,但現在它已更改。 根據文檔,我必須使用Dispatcher 。 在 Github 上找到了最初的想法,但新的簽名是F[Future[A]]
def beforeF[F[_]: Effect, A](fa: F[A]): Future[A] = fa.ioIO.unsafeToFuture()
// Note: Using a `Dispatcher` resource is cheap - don't worry about it
def preferredAfterF[F[_]: Async, A](fa: F[A]): F[Future[A]] = Dispatcher[F].use(_.unsafeToFuture(fa))
有人成功嗎?
構造您的路由,以便它們可以訪問Disptacher
對象作為依賴項,那么生命周期將是適當的
class MyRoute[F[_]: MonadThrow](disp: Dispatcher[F]) {
def method = authed { _ =>
val program: EitherT[IO, Throwable, Int] = ???
disp.unsafeToFuture(program.value)
}
}
(這可能需要在Dispatcher[IO]
資源上使用已allocated
和IORuntime
的 unsafeRunSync 進行一些擺弄,具體取決於您的應用程序的連接方式)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.