Skip to content

Instantly share code, notes, and snippets.

@nha
Last active March 14, 2016 23:05
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 nha/2003f3a5b738e72cb75e to your computer and use it in GitHub Desktop.
Save nha/2003f3a5b738e72cb75e to your computer and use it in GitHub Desktop.
;; MUST be possible
;; http://funcool.github.io/cats/latest/#alet
;; https://gist.github.com/aclemmensen/11272381
;; http://www.lispcast.com/core-async-browser-motivation
;; https://gist.github.com/aclemmensen/11272381
(defn sample-async-fn [t s]
(println "sample-async-fn" t s js/Date.)
(go (<! (timeout t))
s))
(defn sample-async-fn2 [t s]
(let [ch (chan)]
(js/setTimeout (fn []
(put! ch s)) t)
ch))
;; MUST be possible
;; http://funcool.github.io/cats/latest/#alet
;; https://gist.github.com/aclemmensen/11272381
(defn test-core-async-parallel-exec []
(println "BEFORE GO-LOOP")
(let [before (.getTime (js/Date.))]
(go-loop [options {:a "replace-me"
:b "replace-me"}]
;; how can I run this in parallel?
(cond
(= (:a options) "replace-me")
(recur (assoc options :a (<! (sample-async-fn 1000 "ok"))))
(= (:b options) "replace-me")
(recur (assoc options :b (<! (sample-async-fn 2000 "ok"))))
;; all set - exit
;; now all the values are filled in
:default
(js/console.log "DONE " (- (.getTime (js/Date.)) before))))));; roughly 1000 + 2000
(defn test-core-async-parallel-exec2 []
(println "BEFORE GO-LOOP")
(let [before (.getTime (js/Date.))]
(go
(println "IN GO BLOCK")
(println "DONE"
{:a (<! (sample-async-fn2 1000 "ok"))
:b (<! (sample-async-fn2 2000 "ok"))}
(- (.getTime (js/Date.)) before) )))) ;; 3000 too
;;(test-core-async-parallel-exec2)
(defn test-core-async-parallel-exec3 []
(println "BEFORE GO-LOOP")
(let [before (.getTime (js/Date.))
options {:a (sample-async-fn 1000 "ok")
:b (sample-async-fn 2000 "ok")}]
(go
(println "IN GO BLOCK")
(println "DONE" (-> pending
(update :b #(take! %))
(update :a #(take! %)))))))
(test-core-async-parallel-exec3)
;; (println "TEST CORE ASYNC")
;; (test-core-async-parallel-exec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment