Fork me on GitHub
#core-async
<
2019-02-16
>
respatialized17:02:31

I have a question about interacting with core.async from the repl. My goal is to essentially create an async while loop that executes continuously (and prints out a "running..." message every 5 seconds or so) until I either send a message to a channel that tells it to stop or update the state of an atom that invalidates the condition of the wile loop. What's the best way to do this?

jahson18:02:59

I thinking it’ll look like this

(def stop-chan (async/chan))
(async/go-loop []
  (let [[v c] (async/alts! [stop-chan (async/timeout 5000)])]
    (if (or (= c stop-chan) (pos? @atom))
      (do
        (prn "END")
        (async/close! stop-chan))
      (do
        (prn "WORK")
        (recur)))))
=> "WORK"
=> "WORK"
...
(async/close stop-chan)

respatialized19:02:42

This was helpful, thanks!

jahson18:02:17

And for an atom you just need to add another condition and also close stop-chan