将Future [A]转换为Future [Try [A]]或EitherT [Future,Throwable,A]

问题描述

如何将可能成功或失败的Future[A]转换为EitherT[Future,Throwable,A]Future[Try[A]]

future.transform(result => Success(result))
EitherT(future.transform(result => Success(result.toEither)))

标准库中或Cats中是否有实现上述转换的方法?

解决方法

例如,ApplicativeError中的

attemptT可以将Future[A]转换为EitherT[Future,Throwable,A]

import cats.data.EitherT
import cats.implicits._

val f: Future[A] = ???
val v: EitherT[Future,A] = f.attemptT

https://stackoverflow.com/a/63746823/5205022

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...