Last active
April 4, 2018 06:25
-
-
Save adamw/c81a38f7fde846faf2a02f27e770bd89 to your computer and use it in GitHub Desktop.
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
| package test | |
| import scala.concurrent.Future | |
| class Compare3bWrappers { | |
| // data structures | |
| case class Rss(feedAddress: String) | |
| // I/O operations: non-blocking, asynchronous | |
| def fetchFromDb[T](entityClass: Class[T], id: Long): Future[T] = ??? | |
| def sendHttpGet(url: String): Future[String] = ??? | |
| // the business logic: asynchronous | |
| def fetchRssContent(rssIds: List[Long]): Future[List[String]] = { | |
| // all I/O operations will run concurrently | |
| val listOfFutureContents = rssIds | |
| .map(rssId => fetchFromDb(classOf[Rss], rssId)) | |
| .map(_.flatMap(rss => sendHttpGet(rss.feedAddress))) | |
| // combining a list of futures into a future of a list | |
| Future.sequence(listOfFutureContents) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment