Scala Future – Their Uses And How To Implement Them

Reading Time: 3 minutes

A Scala Future is a place holder (an imaginary waiting box) which hold a value that may become available at some point.

Moreover the scala Future API has the following building blocks :
1) Future
2) Promises
3) Execution Context


One of the most important trait of a programming language is to be patient for the code segment to complete their execution asynchronously with maximum possible parallelism.

And we use Future because they facilitate scala with “the art of being patient and non-blocking“.

Asynchronous and parallelism

Asynchronous means no waiting time. The caller function does not wait for a response from the called function; it continues doing its next task, For instance.
Moreover asynchronous function does not block or wait for it’s called function’s response. It will receive response asynchronously.

Parallelism means executing multiple tasks simultaneously, by utilizing the underlying multi-core CPUs effectively. In other wordsthey use multi-core CPUs at a time and execute in a parallel manner.

Moreover In true Parallelism, you need at least two cores and threads need to both be executing at the same time.

When do we use Scala Future ?

The operation which may take time (like I/O) to execute are enclosed under the future scope which starts asynchronous computation and return a Future value holding the result of computation.

We said that Future is an imaginary waiting box , so how to access the resultant value from this box?

The result (value or exception) from a Future is accessible by following ways:-

  • Callbacks :
    We use the Future API callback methods to check the following things from a Future object or a Promise object:
    — Future is complete with a success result or not
    — it is complete with a failure or not
    — it is complete successfully or not completed
    We use the following functions to check the preceding things, respectively:
    onSuccess (deprecated from Scala 2.12.0 onwards)
    onFailure (deprecated from Scala 2.12.0 onwards)
  • Combinators :
    The Scala Future API supports a couple of Scala combinators to ease the way of extracting values from a Future or a Promise object.
    Therefore some of the useful and important Scala Future API combinators in this section:
    — map
    — flatMap
    — recover
    — filter
  • For Comprehensive :
  • Await.result
  • The Async Library (async, await) :
    The Scala Async API does not add any extra features to the Scala Asynchronous Programming API. It is similar to the Scala Future API. However, it allows you to compose a set of Asynchronous computations in a simple and concise way.
    The Scala Async library introduces two new methods—async and await