Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Last active December 18, 2015 11:09
Show Gist options
  • Save martintrojer/5773399 to your computer and use it in GitHub Desktop.
Save martintrojer/5773399 to your computer and use it in GitHub Desktop.
futures
// map creates a new future that is triggered when the originating future is done
scala> future { 42 } map { _ + 1 } map { println(_) }
43
res26: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@5427eb13
// flatMap creates a new future that is triggered when the enclosed future is done
scala> future { 42 } flatMap { v => future { 1 + v } } map { println(_) }
res28: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@9afcbc0
scala> 43
scala> (for { v <- future { 42 }; r <- future { 1 + v } } yield r) onSuccess { case v => println (v) }
scala> 43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment