How to listen to a Clojure promise when it has been implemented or not?

I have a promise of Clojure. When this is realized or fails, I would like to trigger an action. How do I do this in Clojure?

It describes http://dev.clojure.org/display/design/Promises then

and recover

which should make it possible to define such callbacks, but that's not in the core of Clojure as I see it.

+3


source to share


2 answers


Clojure promises cannot fail as they are not tied to a specific thread of execution. See how Clojure futures and promises differ?

For listening to the future or promise Is there a way to get notified when Clojure's future ends? shows a possible option if you don't create another thread.



Other options are to use some Java library like Guava ListenableFuture or look at the new and fancy core.async

+2


source


I recently finished a project where I needed similar functionality from promises and futures in Clojure. After some research, I settled on Cljque . I think this is basically a proof of concept for all the things discussed at http://dev.clojure.org/display/design/Promises . You can visit promises and futures to add callbacks. Then he also secures and restores operations.




If you might want a Java 8 runtime, perhaps you can just write some Clojure wrappers around java.util.concurrent.CompletableFuture .

+1


source







All Articles