If you have been using Java for a while, you might know the java.util.concurrent.Future
class, this is Clojure's implementation of futures and it is extremely similar to Java, only a bit simpler. Its interface and usage are almost identical to promises with a very important difference, when using futures everything will run in a different thread automatically.
Let's see a simple example using futures, in any REPL do the following:
(def f (future (Thread/sleep 20000) "Hello world")) (println @f)
Your REPL should freeze for 20 seconds and then print Hello world
.
Tip
Futures are also cached, so you only need to pay once for the cost of calculation and then you can use them any number of times you want.
At first glance, futures look much easier than promises. You don't need to worry about creating threads or fibers, but there are downsides to this approach:
You have less flexibility; you can only run futures in a predefined thread pool.
You should be careful if your futures take too much time...