philipbohun.com

Glojure by Example: Select

alt! waits on several channel operations and takes the first one that can proceed. It is core.async's select. Each clause is a channel operation and a result expression. A result that starts with a binding vector receives the value.

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

(def one (async/chan))
(def two (async/chan))
(async/go (async/>! one "one"))
(println (async/alt!
           one ([v] v)
           two ([v] v)))

Only one ever receives a value, so the choice is determined. When several operations are ready, alt! picks one of them. There is no directional channel type in this API: every core.async channel can send and receive.

The send on one completes the alt!.

$ glj select.glj
one

Next example: Timeouts.