Created
April 18, 2018 07:44
-
-
Save Yyukan/55628f308e59ca1d9503c158f1717c85 to your computer and use it in GitHub Desktop.
Example of using EitherT monad transformer with Future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.implicits._ | |
import cats.data.EitherT | |
import scala.concurrent.Future | |
import scala.concurrent.Await | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
trait ServiceError | |
case object Timeout extends ServiceError | |
type EitherFuture[A] = EitherT[Future, ServiceError, A] | |
def callFirstService: EitherFuture[String] = { | |
EitherT[Future, ServiceError, String]( | |
Future.successful(Right("Hello")) | |
) | |
} | |
def callSecondService: EitherFuture[String] = { | |
EitherT[Future, ServiceError, String]( | |
Future.successful(Right("World !")) | |
) | |
} | |
val result1: EitherFuture[String] = callFirstService | |
val result2: EitherFuture[String] = callSecondService | |
val result = for { | |
r1 <- result1 | |
r2 <- result2 | |
} yield r1 + r2 | |
Await.result(result.value, 1 seconds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment