Skip to content

Instantly share code, notes, and snippets.

@agaro1121
Created July 26, 2019 15:23
Show Gist options
  • Save agaro1121/7e71a3a3a0174b1b8b9702c587e7ddcd to your computer and use it in GitHub Desktop.
Save agaro1121/7e71a3a3a0174b1b8b9702c587e7ddcd to your computer and use it in GitHub Desktop.
Future Run semantics
def bar(f: => Future[Any]): Future[Any] = {Thread.sleep(1000); f}

bar(Future(blah)) // runs the future when `f` is reached in bar, 1 second after the call is made.

val foo = Future(blah) // future starts now
bar(foo)

def bar(f: => Future[Unit]): Future[Unit] = {f; f}

bar(Future(println("foo"))
// prints foo twice

val baz = Future(println("woozle"))
bar(baz)
// prints foo once.

val foo = Future(bar)
val woozle = Future(wozzle)

for { 
  x <- foo
  y <- woozle
} yield (x, y)
// runs the futures in parallel

for {
  x <- Future(bar)
  y <- Future(wozzle)
} yield (x, y)
// runs the futures sequentially
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment