Someone can explain me why the behavior of Twitter future is not Async? Having this code
private val future: Future[String] = Future {
Thread.sleep(2000)
println(s"Running this effect in ${Thread.currentThread().getName}")
"Hello from Twitter"
}
println("Present")
Await.result(future)
The output after two seconds is this one
Running this effect in main
Present
So the future is blocking and not running in another Thread
Twitter futures have somewhat different semantics from "standard" futures in scala.
Future.applyis actually analogous to standard scala'sFuture.successful: it creates aFuturethat is already satisfied – so, the block you pass in is executed immediately.With Twitter Futures, you don't need the annoying
ExecutionContextthat you have to drag implicitly everywhere. Instead, you have aFuturePoolto which you hand code explicitly to be asynchronously executed. E.g.:The good thing about this is that, unlike
scala.concurrent, thisFutureis self-contained: it "knows" about the pool managing it, so, you don't need to carry it around everywhere thisFuturegoes in case someone will need to domaporflatMapor some other transformation on it.