Created
November 2, 2013 22:54
-
-
Save gerritjvv/7284388 to your computer and use it in GitHub Desktop.
A offer function that will timeout if the offer cannot be made
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
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