MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Futures

Examples

Creating a Future


import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object FutureDivider {
 def divide(a: Int, b: Int): Future[Int] = Future {
 // Note that this is integer division.
 a / b
 }
}

Quite simply, the divide method creates a Future that will resolve with the quotient of a over b.

Consuming a Successful Future


The easiest way to consume a successful Future-- or rather, get the value inside the Future-- is to use the map method. Suppose some code calls the divide method of the FutureDivider object from the "Creating a Future" example. What would the code need to look like to get the quotient of a over b?

object Calculator {
 def calculateAndReport(a: Int, b: Int) = {
 val eventualQuotient = FutureDivider divide(a, b)
 eventualQuotient map {
 quotient => println(quotient)
 }
 }
}

Consuming a Failed Future


Sometimes the computation in a Future can create an exception, which will cause the Future to fail. In the "Creating a Future" example, what if the calling code passed 55 and 0 to the divide method? It'd throw an ArithmeticException after trying to divide by zero, of course. How would that be handled in consuming code? There are actually a handful of ways to deal with failures.

Handle the exception with recover and pattern matching.

object Calculator {
 def calculateAndReport(a: Int, b: Int) = {
 val eventualQuotient = FutureDivider divide(a, b)
 eventualQuotient recover {
 case ex: ArithmeticException => println(s"It failed with: ${ex.getMessage}")
}
 }
}

Handle the exception with the failed projection, where the exception becomes the value of the Future:

object Calculator {
 def calculateAndReport(a: Int, b: Int) = {
 val eventualQuotient = FutureDivider divide(a, b)
 // Note the use of the dot operator to get the failed projection and map it.
 eventualQuotient.failed.map {
 ex => println(s"It failed with: ${ex.getMessage}")
 }
 }
}



Conclusion

In this page (written and validated by ) you learned about Scala Futures . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Handling units.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.