Skip to content

Instantly share code, notes, and snippets.

@gilbertw1
Last active June 9, 2017 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilbertw1/8bc5898e8be292fb4164 to your computer and use it in GitHub Desktop.
Save gilbertw1/8bc5898e8be292fb4164 to your computer and use it in GitHub Desktop.
Composing Dependent Futures
// 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