Scala 2.12 combination of Future and Either (biased version)

Viewed 937

I have two methods and both returns for Future[Either[String, T]] and want to chain them together to get the job done. I am using Scala 2.12 Either is right biased now.

For example,

If the function not return Future and Either combination, I can simply do like following with for comprehension.

def convert(a, b) = ???
def callA(): Future[A] = ???
def callB(a: A): Future[B] = ???

def chain: Future[C] = for {
  a <- callA
  b <- callB(a)
} yield convert(a, b)

but now I want to capture the error using Either with following syntax, and propagate to the final result. what's the best way to achieve that? Also should callB or callB1 used?

def convert(a, b): Future[Either[String, C]] = ???
def callA(): Future[Either[String, A]] = ???
def callB(a: A): Future[Either[String, B]] = ??? or 
def callB1(eithera: Either[String, A]): Future[Either[String, B]] = ???

I know using cats transform monad could simplify this. Welcome to give me solution for both using standard lib and cats.

1 Answers
Related