3

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 Answer 1

8

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
  )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.