Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Introduce a flip method: it looks tricky, but as usual it just "follows the types":
- private def flip[L, R](eitherFuture: Either[L, Future[Either[L, R]]]): Future[Either[L, R]] = {
- eitherFuture.fold[Future[Either[L, Either[L, R]]]](
- left => Future.successful[Either[L, Either[L, R]]](Left(left)),
- right => right.map(Right(_))
- ).map(_.joinRight)
- }
- // Now we can count on flapMap to do the magic!
- def processRequest(a: A): Future[Result] = {
- for {
- b <- f1(a)
- c <- flip(b.map(f2))
- d <- flip(c.map(f3))
- r <- flip(d.map(f4))
- } yield r.getOrElse[Result](r.left.get)
- }
Add Comment
Please, Sign In to add comment