This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-26
Channels
- # beginners (74)
- # cider (3)
- # cljsrn (1)
- # clojure (182)
- # clojure-dev (4)
- # clojure-europe (1)
- # clojure-spec (5)
- # clojure-uk (58)
- # clojurescript (44)
- # core-async (5)
- # core-logic (10)
- # cursive (6)
- # datomic (13)
- # duct (1)
- # fulcro (2)
- # graalvm (4)
- # leiningen (1)
- # off-topic (7)
- # overtone (6)
- # random (3)
- # re-frame (17)
- # reitit (2)
- # shadow-cljs (6)
- # spacemacs (4)
- # sql (12)
- # tools-deps (3)
I have a small problem I want to tackle with clojure.async. I have an API I need to get resources from, but the results are paginated and I can only get the next page after receiving a url from the previous page. I want to get each page, then merge the results with the last page into a map.
(defn get-data-async
[req]
(let [in-c (async/chan)
out-c (async/chan)]
;; Blocking HTTP gets are run in a thread, putting results into in-c as they come in
(async/thread
(loop [current-req req]
(if (nil? current-req)
(async/close! in-c)
(let [result (get-result current-req)]
(async/>!! in-c result)
(recur (next-req result))))))
;; Process data as it arrives into a single map; result is put into out-c
(async/go-loop [result (async/<! in-c)
collector nil]
(if (nil? result)
(async/>! out-c collector)
(recur (async/<! in-c) (merge-result collector result))))
out-c))
Was hoping I could get input on how to make this better. Appreciate any help!Why sending :stop instead of closing the channel, which is the standard way to signal end? While reading <!! If nil you know you should send the collected result.
@U0522TWDA Thanks, fixing my code to close channel instead
Take unfold from https://clojure.atlassian.net/browse/CLJ-1906 rewrite it to produce items on a channel instead of a seq and to treat the generating function as returning a channel
thanks for sharing this!