Table of Contents
- Run one task, but block
- Run one thing, but don’t block, use callback
- The onSuccess and onFailure callback methods
- Creating a method to return a Future[T]
- How to use multiple Futures in a for loop
- A future and ExecutionContext
- Callback methods
- For-comprehensions (combinators: map, flatMap, filter, foreach, recoverWith, fallbackTo, andThen)
- See Also
You want a simple way to run one or more tasks concurrently in a Scala application, including a way to handle their results when the tasks finish. For instance, you may want to make several web service calls in parallel, and then work with their results after they all return.Back to top
Future gives you a simple way to run an algorithm concurrently. A future starts running concurrently when you create it and returns a result at some point, well, in the future. In Scala, it’s said that a future returns “eventually.”
As a quick note to self, this is what I had to do to get a “Delete” menu item working on a JavaFX TableView row:
I’ve currently written this document as a “note to self” about how the Android
AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the
AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an
AsyncTask is a little weak atm.
“Haskell functions can take functions as parameters and return functions as return values. A function that does either of those is called a higher order function. Higher order functions aren’t just a part of the Haskell experience, they pretty much are the Haskell experience.”
I can tell you from my own recent experience that if you don’t use the Sencha ExtJS Store load method properly, nothing will happen. After torturing myself twice by mishandling the load method, I thought maybe it would help if I wrote a quick blog post about how to properly use the load method.
There are a number of ways to work with Scala Futures, and I provide examples of those approaches in the Scala Cookbook.
If you’re new to Futures, a fun exercise is to try to determine what happens in your code when you use a certain technique. For instance, when you look at the following Scala source code, what do you think it will print?
I've posted a lot of Scala source code examples out here lately, and as I keep trying to learn more about passing one function to another function in Scala (function callbacks), here's another example showing how you can use Scala's functional programming approach to clean up some Java Swing code:
Note: The following examples of passing a function as an argument to another function have all been taken from the PDFs on the Scala website. The only thing I've done here is to add comments to the source code, and add detailed discussions of them in this article.