Created
August 9, 2013 20:09
-
-
Save logosity/6196767 to your computer and use it in GitHub Desktop.
Block a caller on any number of long-running/non-terminating functions continuing when any one of them returns (the others will run to completion, or presumably get cleaned up by the calling context):
This file contains hidden or 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
;;; block caller until any one of the passed functions complets and return its result | |
(defn wait-any [& fns] | |
(let [p (promise)] | |
(doseq [f fns] (future (deliver p (f)))) | |
(deref p))) | |
;;; (wait-any #(do (Thread/sleep 4000) 1) #(do (Thread/sleep 2000) 2) #(do (Thread/sleep 3000) 3)) | |
;;; output: 2 |
swannodette
commented
Aug 9, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment