3
votes

I have a go-loop reading vals from a port in ClojureScript. After a pre-defined timeout (say, 5 seconds) I need to exit the loop and if no vals appeared for more than another predefined timeout (say, 200 milliseconds) I also need to exit the loop.

By exciting the loop I mean, end it and run something else, another function for example.

Is this possible?

1

1 Answers

8
votes

Use alts!

;; within your go-loop:

(let [[val ch] (alts! [my-ch (timeout 500)])]
  (when (identical? my-ch ch)
    ;; dispatch val here...
    (recur))
  ;; not calling recur here, to exit the loop
  )