philipbohun.com

Glojure by Example: Timeouts

timeout returns a channel that closes after the given milliseconds. A receive on a closed channel yields nil, so an alt! clause on it runs when the deadline passes.

(require '[clojure.core.async :as async])

(def answers (async/chan))
(println (async/alt!
           answers ([v] v)
           (async/timeout 30) ([_] "timed out")))

Nothing is sent on answers, so the timeout wins.

$ glj timeouts.glj
timed out

Next example: Non-Blocking Operations.