core.async machine that loops until a fx returns true <p>While loading a web site, I often have the need to load code AFTER some other DOM structural event has occurred. Therefore I often end up with a lot of functions that first check if some element exist in the DOM and then do their thing in the event that said element exist.</p> <pre><code>(ns example.core (:require [cljs.core.async :as async] goog.async.AnimationDelay) (:require-macros [cljs.core.async.macros :refer [go go-loop]])) (defn register-click-handler [] (when js/window.document.body (goog.events.listen js/window.document.body goog.events.EventType.CLICK #(js/console.log "I was clicked!")) true)) (defn loop-until-true [fx] (let [c (async/chan) f (goog.async.AnimationDelay. #(async/put! c :loop))] (async/put! c :initial-loop) (go-loop [] (async/&lt;! c) (when-not (true? (fx)) (.start f) (recur))))) (loop-until-true register-click-handler) </code></pre> <p>I think I like this pattern as it lets me write <code>fx</code> that keeps trying until the DOM is in a state where the function can succeed (return true); without having to go through a great deal of ceremony in terms of dealing with retrying (just return true on success).</p> <p>I am looking for improvements to <code>loop-until-true</code>, a function that registers another function to keep executing until that function returns true. I don't want to actually block the execution of the code that might cause <code>fx</code> to pass, so I am using a core.async block to IOC the code. This code seems to work, but I am looking for improvements and critics.</p>