Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(require '[clojure.core.async.impl.protocols :as impl]
'[clojure.core.async.impl.dispatch :as dispatch])
;;=> nil
(extend-type java.util.concurrent.CompletionStage
impl/ReadPort
(take! [this handler]
(.whenCompleteAsync this
(reify
java.util.function.BiConsumer
(accept [_ result exception]
(.lock handler)
(let [good (and (impl/active? handler)
(impl/commit handler))]
(.unlock handler)
(when good
(dispatch/run #(good (or exception result))))))))
nil))
;;=> nil
(require '[clojure.core.async :as async])
;;=> nil
(def cf1 (java.util.concurrent.CompletableFuture.))
;;=> #'user/cf1
(first (async/alts!! [(async/timeout 1000) cf1]))
;;=> nil
(.complete cf1 "Foo")
;;=> true
(first (async/alts!! [(async/timeout 1000) cf1]))
;;=> "Foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment