我有Future[Either[A, B]]
,并且函数B
提供Future[C]
。使用(B => Future [C])函数Future [[A,B]]到将来[要么[A,C]]
我需要将Future[Either[A, B]]
转换为Future[Either[A, C]]
。
是否有直接的方式获得Future[Either[A, C]]
而不是Future[Either[A, Future[C]]]
?
我想是这样的:
val eventuallyInitialValue: Future[Either[A, B]] = ???
val result: Future[Either[A, C]] = for {
e: Either[A, B] <- initialValue
c: C <- service.getValue(e.right)
} yield e.right.map(_ => c)
它,因为service.getValue(e.right)
只是伪代码不能编译。什么是正确的做法?
感谢。它只是需要返回作为一个右:bToFC(r).map(右(_)) –
@Alban:是的,编辑在 – Daenyth
如果您使用'Future.successful'而不是'Future.apply'您将避免在线程池上创建'Left(e)'。 –