Last active
June 9, 2017 09:23
-
-
Save gilbertw1/8bc5898e8be292fb4164 to your computer and use it in GitHub Desktop.
Composing Dependent Futures
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
// filter out empty options and unbox | |
def transform[T](fut: Future[Option[T]]): Future[T] = { | |
fut.filter(_.isDefined).map(_.get) | |
} | |
val result = for ( | |
foo <- transform(fooService.get(1)); | |
bar <- transform(barService.get(foo.barId)); | |
quux <- transform(quuxService.get(bar.quuxId)) | |
) yield(Ok("Quux = " + quux)) | |
// If future is empty return not found, other errors result in internal server error | |
result recover { | |
case e: NoSuchElementException => NotFound | |
case _ => InternalServerError | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment