Created
January 5, 2021 07:22
-
-
Save raspasov/50ca55a236d14338d3736b91a0d1a7e1 to your computer and use it in GitHub Desktop.
core.async 101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(comment | |
;This takes at most ~2000 ms to execute | |
(let [process-seq-of-channels | |
(fn [chans] | |
(a/go | |
(loop [chans chans] | |
(if-not (empty? chans) | |
(let [ch (first chans) | |
resp (a/<! ch)] | |
(println resp) | |
(recur (rest chans))) | |
(println :done))))) | |
fake-http! | |
(fn [req idx] | |
(let [resp-ch (a/chan 1)] | |
;TODO Do HTTP request!!! | |
(a/go | |
(a/<! (a/timeout (rand-int 2000))) | |
(a/put! resp-ch {:resp req :idx idx})) | |
;TODO end | |
;return channel | |
resp-ch))] | |
(a/go | |
;initiate requests, replace fake-http! with a function that makes an HTTP request and returns a channel | |
;MUST use (doall...) to send the requests right away! | |
(let [resp-chans (doall (map fake-http! (repeat 10 {:req 1}) (range 10)))] | |
(println :starting) | |
(process-seq-of-channels resp-chans))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment