Futures in Scala are an extension of Futures in Java. They allow us to execute multiple tasks in parallel in a very efficient and non-blocking manner. Just like in Java, the Future is a placeholder of a result of a computation that has been run asynchronously. Normally, the result of the asynchronous computation is supplied to the Future in a concurrent manner. This results in a non-blocking asynchronous task. Scala Futures provide methods that can be used to check when the concurrent task is finished.
The default behavior of Futures and Promises is that they are non-blocking operations and provide callbacks for getting the results of the asynchronous task. In Scala, we can make use of methods such as flatMap
, filter
, and foreach
to use the Futures in a non-blocking manner.
Let's look at some examples here:
Val studentFuture:Future[StudentDetails] { // non-blocking rest call or computation studentService.studentDetails('Student 101') }...