core-async

erwinrooijakkers 2019-12-21T15:58:57.029400Z

I try to read all values from a channel that arive in a second:

(loop [outputs []]
    (let [[v p] (alts!! [out (timeout 1000)])]
      (if v
        (recur (conj outputs v))
        outputs)))

erwinrooijakkers 2019-12-21T15:59:11.029900Z

This returns [nil nil nil]

erwinrooijakkers 2019-12-21T15:59:13.030100Z

erwinrooijakkers 2019-12-21T15:59:20.030400Z

Is the code wrong or is there nothing on the out channel?

dpsutton 2019-12-21T16:17:39.031100Z

just off the cuff, you need the timout outside the loop. otherwise you get a new one-second window each time you get a value

erwinrooijakkers 2019-12-21T16:19:17.031700Z

Thanks. The new time window is fine. n amount of values will arrive and then stop

dpsutton 2019-12-21T16:31:11.032400Z

scratch.random> (let [out (doto (a/chan 5)
                            (a/>!! :value)
                            (a/>!! :value)
                            (a/>!! :value))]
                  (loop [outputs []]
                    (let [[v p] (a/alts!! [out (a/timeout 1000)])]
                      (if v
                        (recur (conj outputs v))
                        outputs))))
[:value :value :value]

dpsutton 2019-12-21T16:31:18.032600Z

it seems to work for me.

erwinrooijakkers 2019-12-21T17:42:53.032800Z

I see, hmm thanks