scala - 将 EitherT[Future, A, Future[B]] 转换为 EitherT[Future, A, B]
问题描述
我正在尝试更改EitherT[Future, A, B]
toEitherT[Future, C, D]
并且为此,我正在使用bimap
适当地映射左右部分。当我正在转换它的正确部分时EitherT
,我正在做一个服务调用,它返回给我一个Future[D]
……我在将它转换Future[D]
为D
我的bimap
. 现在不知道如何进行。这里的任何帮助将不胜感激。
伪代码:
val myResult: EitherT[Future, C, D] = EitherT[Future, A, B](myService.doStuff())
.bimap({ err => /*deal with errors and give me C*/ }
,{ success => someService.doSomething(success) // This is returing a Future[D]. But I want a D
})
解决方案
尝试.flatMap
又名for
- 理解
import cats.data.EitherT
import cats.instances.future._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
val myResult: EitherT[Future, C, D] = for {
d <- EitherT.right(someService.doSomething())
res <- EitherT[Future, A, B](myService.doStuff())
.bimap({ err => ??? : C //deal with errors and give me C
}, { success => {
d
}
})
} yield res
尝试.biSemiflatMap
val myResult: EitherT[Future, C, D] =
EitherT[Future, A, B](myService.doStuff())
.biSemiflatMap({ err => Future.successful(??? : C)
}, { success => {
someService.doSomething(success)
}
})
推荐阅读
- sql - 计算另一列中某个值出现次数的存储过程
- python - 多个字段包括用 python 和 mysql 排除
- arrays - VBA 数组函数的工作速度比 vlookup 慢
- tarantool - 如何向现有索引添加新字段?
- list - 我想在颤振中为购物应用程序制作类别页面?
- python - 与anaconda的包冲突
- php - 调用带有参数的控制器?
- xcode - 如何在 Xcode 12 的新更新中显示视图控件?
- linux - 带有 ssh 的 Git 克隆适用于 Linux,但不适用于 Windows PowerShell
- javascript - Tinymce 编辑器只添加文本