goral
goral

Reputation: 1265

How are Futures in Scala evaluated, computed

I was thinking, how are Futures being evaluated? I mean if we have imperative style programming where we execute program from point A to point B and somewhere between them we create a Future which, when completed, prints the result to the console. How is our program making this step back in flow to print it?

Upvotes: 0

Views: 105

Answers (1)

johanandren
johanandren

Reputation: 11479

Futures are run on an ExecutionContext which is essentially a threadpool. When you create a Future block or use the various composition and callback methods (map, foreach, onComplete etc) on a future there is an implicit execution context passed along where the logic will be executed.

In a imperative program this will be roughly the same as just pushing a Runnable that would print onto the console onto a threadpool.

There is a good introduction here: http://docs.scala-lang.org/overviews/core/futures.html

The most useful thing about futures is not using them imperatively though, but instead composing using map and flatMap to create chains of futures.

Upvotes: 1

Related Questions