Promises and Futures in Clojure

1 min read Original article ↗

Promises and Futures in Clojure

At the moment, it’s pretty hard to read about what is a promise or a future. In this post, I’m going to try to explain them concisely.

Futures run some code on a newly spawned worker thread, and allow you to wait for the result of that code’s execution.

Promises are a kind of reference that can be set exactly once and allow you to wait for them to be set.

Futures take the expression that’s their argument and run it in another thread. This thread is actually the cachedThreadPool used by send-off for Agents. The IDeref protocol (that’s what lets you say @future to get the value) is implemented by using the underlying Java Future’s get method.

Promises are essentially one-shot atoms that have a CountDownLatch (counting down from 1) to allow other threads to block when they try to @deref the promise until it’s been delivered.