Scala (19) — Futures

haimei
play-hard-work-hard
1 min readApr 21, 2016

Futures

  • They hold the promise for the result of a computation that is not yet complete. They are a simple container- a placeholder. A computation could fail of course, and this must also be encoded. a Future can be in exactly one of 3 states:
  • pending
  • failed
  • completed
  • With flatMap we can define a Future that is the result of two futures sequenced, the second future computed based on the result of the first one.
  • Future defines many useful methods:
  • Use Future.value() and Future.exception() to create pre-satisfied futures
  • Future.collect(), Future.join() and Future.select() provide combinators that turn many futures into one (i.e. the gather part of a scatter-gather operation)
  • By default, futures and promises are non-blocking, making use of callbacks instead of typical blocking operations. Scala provides combinators such as flatMap, foreach and filter used to compose futures in a non-blocking method.

--

--