Fork me on GitHub
#core-async
<
2018-09-28
>
basti17:09:10

I’m fairly new to Clojure and I’ve got a question regarding core.async channels. I’m using a http library which returns a channel - Now I want to do two http request at the same time and then “wait” for both to complete. How do I achieve such?

noisesmith17:09:32

do you need to wait for both before the next step?

noisesmith17:09:07

if so, you can call <!! on each channel to get the results

basti17:09:22

okay cool I think I know how I can sync the operations now! 🙂

noisesmith18:09:00

core.async has many options for asynchronous coordination, but if reading results in order as they are available works, it's probably the simplest option

👍 4
basti18:09:08

so kinda like

(let [ch1 (http-request-1)
        ch2 (http-request-2)
        response1 (<!! ch1)
        response2 (<!! ch2)]
  (process-both-results response1 response2))

basti18:09:39

thanks for the help all!

basti18:09:15

One more thing - is there an easy way to add a timeout on <!! ?

noisesmith18:09:40

there's no way to do that directly, it might be easiest to use alts!! with a timeout-chan in that case

basti18:09:27

awesome that sounds promising! will look into that!

noisesmith18:09:13

replace (<!! c) with (<!! (alts!! [c (timeout N)])), then you need to test if the result is the timeout or the http result

noisesmith18:09:32

you're almost at the point of complexity where you might need a go block, but I don't think it's there yet