How to call `thenRun` method of a completable Future in Clojure?

I need to interop with java code that returns a CompletableFuture. I want to call the thenRun method of CompletableFuture. I am not sure how to do it with clojure.

(.thenRun future ???)
(.thenApply future ???)

In general, how do you deal with functions defined in java.util.functions in clojure.

You should be able to do that using reify – see https://github.com/worldsingles/commons/blob/master/src/ws/clojure/extensions.clj#L124-L150 for some syntactic sugar we’ve written around CompletableFuture.

1 Like

Clojure function casts fine to the Runnable.
The Function can be declared with reify.

  (let [fu (CompletableFuture.)]
    (.thenRun fu (fn [] (doto :then-run println)))
    (.thenApply fu (reify Function
                     (apply [_ x] (doto :then-apply (println x)))))
    (.complete fu :complete))
:then-apply :complete
:then-run
=> true
2 Likes