Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Created November 2, 2013 22:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerritjvv/7284388 to your computer and use it in GitHub Desktop.
Save gerritjvv/7284388 to your computer and use it in GitHub Desktop.
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
@gerritjvv
Copy link
Author

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