Fork me on GitHub
#core-async
<
2017-04-04
>
danielgrosse12:04:14

When I run (go (while true (prn (<! channel)))shouldn't it stop, when the channel is empty? In my case its repeats printing nil until i kill the process. I use core.async under cljs.

dergutemoritz12:04:12

@danielgrosse You need to make the loop condition depend on whether the channel is closed (i.e. whether a take returned nil)

danielgrosse12:04:35

Like (go (while (not (nil?)) (foo)))?

dergutemoritz12:04:34

Yeah but while is not a good fit here, better go with loop/`recur`

dergutemoritz12:04:37

Like (go (loop [] (when-some [x (<! channel)] (prn x) (recur)))), for example

dergutemoritz12:04:37

Or use the go-loop shorthand if you like

dergutemoritz12:04:40

With while you couldn't use the value read from the channel in case it isn't yet closed and exhausted

danielgrosse12:04:06

Thank you, when-some was the clue.

noisesmith17:04:55

@danielgrosse I use this

(defmacro while-let
  [test-binding & body]
  `(loop []
     (when-let ~test-binding
       ~@body
       (recur))))

noisesmith17:04:02

for exactly that sort of scenario