Both futures and promises block until they have calculated their values, so what is the difference between them?
In Clojure, promise
, future
, and delay
are promise-like objects. They all represent a computation that clients can await by using deref
(or @
). Clients reuse the result, so that the computation is not run several times.
They differ in the way the computation is performed:
future
will start the computation in a different worker thread. deref
will block until the result is ready.
delay
will perform the computation lazily, when the first client uses deref
, or force
.
promise
offers most flexibility, as its result is delivered in any custom way by using deliver
. You use it when neither future
or delay
match your use case.