philipbohun.com

Glojure by Example: Non-Blocking Operations

:default on alts! returns immediately when no other operation is ready. The result is a vector of the value and the channel that was chosen, or the keyword :default.

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

(def messages (async/chan))
(println (async/alts! [messages] :default "no message"))

offer! sends only if the channel can take the value now. It returns true on success and nil if the send would block. poll! receives only when a value is already waiting. Glojure channels throw if you send on a closed channel. They do not ignore that send the way JVM core.async does.

(def buffered (async/chan 1))
(println (async/offer! buffered "a"))
(println (async/offer! buffered "b"))
(println (async/poll! buffered))
(println (async/poll! messages))

The second offer does not fit in a buffer of one.

$ glj non-blocking-operations.glj
[no message :default]
true
nil
a
nil

Next example: Closing Channels.