Over a million developers have joined DZone.

Transforming Either[Future[A],Future[B]] to Future[Either[A,B]]

DZone's Guide to

Transforming Either[Future[A],Future[B]] to Future[Either[A,B]]

Are your Scala Futures and Eithers getting tangled? Removing the Future and flattening everything out not working? See how the Cats library can help.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Recently, I came across one weird situation in which I was getting a Future[Either[String , Future[A]]]. It was getting more and more complex, so I thought about taking out the Future from the Either and then flattening it out, as most of us do anyway. 

But this time, this simple trick was not working, which led to me getting frustrated. Then, while searching for a solution, I came across a very good library for Scala known as Cats, which seemed to solve my problem.

Just FYI, Cats is a library that provides abstractions for functional programming in the Scala programming language. The name is a playful shortening of the word category (copied from GitHub).

So, this is how I was introduced to Cats! I solved my problem is via writing a function using a cats.Functor.

To use Cats, you need to add the cats dependency in your build.sbt 

name := "scala-cats"
version := "0.1"
organization :="com.internity"
scalaVersion := "2.12.3"
scalacOptions += "-Ypartial-unification"
libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.0-RC1"

case class A(a:String)

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import cats.implicits._
import cats.Functor

val eitherOfFutures:Either[Future[String], Future[A]]=Right(Future.successful(A("Shiv")))
val futureOfEitherOfFuture=Future.successful(eitherOfFutures)

def eitherFmap[F[_], A, B](either: Either[F[A], F[B]])(implicit F: Functor[F]): F[Either[A, B]] = either.fold(f => F.map(f)(Left(_)), f => F.map(f)(Right(_)))

val futureOfEither=futureOfEitherOfFuture.flatMap(eitherFmap(_))

You can also see it on the sbt console:


So this is how we can convert Either[Future[A],Future[B]] into Future[Either[A,B]].

But a comment from M50d pointed out an easier solution.

You can also achieve what we did in a simpler way by using a bisequence in cats.instances._, as pointed out by M50d

import cats.instances._


If you have any problems, look me up on Twitter and let me know!

On an unrelated note, I am also working on a project to create community of developers to bridge the gap between corporations and colleges! If you want to be a part of this movement, sign up on InternityFoundation.

Until then, happy hAKKAing!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

scala ,functional programing ,cats ,java ,future ,either ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}