0

How can I transform a Future[A] that might succeed or fail into a EitherT[Future, Throwable, A] or a Future[Try[A]].

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

Are there methods in the standard library or within Cats that achieve the above transformation?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Aki
  • 1,644
  • 12
  • 24
  • 1
    Is `future.transform(Success(_))` not good? It is very simple and why do you need another library to do that? – SwiftMango Sep 07 '20 at 19:08
  • I've expected Cats to have some utility function that simplified further processing. Right now I have for instance much code like this `OptionT(future.transform(result => Success(result.toOption))))` – Aki Sep 07 '20 at 19:11
  • @texasbruce I've clarified the question. This is more specific what I expected, The question was initially asked a bit unfavorably. – Aki Sep 07 '20 at 19:25

1 Answers1

2

attemptT of ApplicativeError can convert Future[A] to EitherT[Future, Throwable, A], for example

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

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

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

Mario Galic
  • 47,285
  • 6
  • 56
  • 98