Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A offer function that will timeout if the offer cannot be made
(use 'clojure.core.async)
(defn offer [ch msg timeout-ms]
"Blocks till the message can be placed on the queue and returns true otherwise returns false"
(not
(nil?
(first (alts!! [(go (>! ch msg) msg) (timeout timeout-ms)])))))
;;lets test
(= false (offer (chan) 1 100))
;=> true
(= true (offer (chan 10) 1 100))
;=> true
@thegeez
Copy link

thegeez commented Nov 3, 2013

(defn offer [ch msg timeout-ms]
  (alt!!
   [[ch msg]] true
   (timeout timeout-ms) false))

@gerritjvv
Copy link
Author

gerritjvv commented Nov 3, 2013

even shorter thanks.

I did not know that you could send alt!! a [[ch msg]] resp argument sequence and have it call the channel with the correct message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment