Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parallel Composition of Scala Futures
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Author(id: Long, name: String)
case class Publication(id: Long, authorId: Long, title:String)
case class AuthorPublications(author: Author, publications: List[Publication])
def findAuthor(query: String): Future[Long] = ???
def getAuthor(id: Long): Future[Author] = ???
def getPublications(authorId: Long): Future[List[Publication]] = ???
def findPublications(query: String): Future[AuthorPublications] =
for {
authorId <- findAuthor(query)
author = getAuthor(authorId)
pubs = getPublications(authorId)
(author, pubs) <- for {
author <- author
pubs <- pubs
} yield (author, pubs)
} yield AuthorPublications(author, pubs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.