(require '[clojure.core.async :as async])
; create channel
(def c (async/chan))
; create promise
(def d (promise))
; record high-res timestamp;
; place value (1) on channel;
; in a callback, record duration of time between enqueueing and dequeueing;
; resolve promise with that value
(let [t (. System (nanoTime))]
(async/put! c 1
(fn [_]
(deliver d (- (. System (nanoTime)) t)))))
; do a blocking take from the channel, do a blocking resolve of the promise
(println "taken value, delivery time" [(async/<!! c) @d])
; same as above but with go block
(def tc
(let [t (. System (nanoTime))]
(async/put! c 2)
(async/go [(async/<! c) (- (. System (nanoTime)) t)])))
; go blocks return a channel immediately,
; the actual return value of the go block gets placed on the channel when ready
(println "taken value, delivery time" (async/<!! tc))
;; taken value, delivery time [1 1191935]
;; taken value, delivery time [2 1668001]