Skip to content

Instantly share code, notes, and snippets.

@raspasov
Created January 5, 2021 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspasov/50ca55a236d14338d3736b91a0d1a7e1 to your computer and use it in GitHub Desktop.
Save raspasov/50ca55a236d14338d3736b91a0d1a7e1 to your computer and use it in GitHub Desktop.
core.async 101
(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