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

Transform Future[A] to Future[Try[A]] or 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, Throwable, A] = f.attemptT