Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active April 4, 2018 06:25
Show Gist options
  • Select an option

  • Save adamw/c81a38f7fde846faf2a02f27e770bd89 to your computer and use it in GitHub Desktop.

Select an option

Save adamw/c81a38f7fde846faf2a02f27e770bd89 to your computer and use it in GitHub Desktop.
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