3

I have a Clojure promise. When it's realized or fails I'd like to fire an action. How to do it in Clojure?

Here: http://dev.clojure.org/display/design/Promises then and recover is described which should give a possibility to define such callbacks, but it is not in the clojure core as I see it.

fifigyuri
  • 5,771
  • 8
  • 30
  • 50

2 Answers2

2

Clojure promises cannot fail as they are not tied to a particular execution thread. Have a quick look at How do Clojure futures and promises differ?

For listen to either a future or a promise Is there a way to be notified when a clojure future finishes? shows a posible option if you don't mind creating another thread.

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

Community
  • 1
  • 1
DanLebrero
  • 8,545
  • 1
  • 29
  • 30
1

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 it's basically a proof-of-concept of all the things discussed on http://dev.clojure.org/display/design/Promises. You can attend promises and futures to add a callback. It also provides then and recover operations.


If you can require the Java 8 runtime, then maybe you can just write some Clojure-style wrappers around java.util.concurrent.CompletableFuture.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • It is archived and experimental. Probably it's not good to build on it. Thanks for pointing to it though. – fifigyuri Aug 05 '14 at 16:39
  • @fifigyuri - I didn't have any problems with it—but that doesn't mean there aren't any bugs. However, using Clique would probably be less-error-prone than rolling out your own solution from scratch, even if it is "experimental". If you can find something that's actively maintained that would probably be better, but if in the end you can't find anything, at least this gives you a decent starting point. Also, see my edit. – DaoWen Aug 05 '14 at 17:32