This is a simple node.js core.async program but it still confuses me.
needed-answers is 25 but I can only get 4 answers. Nevertheless the program
terminates (btw without any output), why ?
(ns crawler.core
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [cljs.nodejs :as node]
[cljs.core.async :as async :refer [>! <! put! chan alts!]]))
(node/enable-util-print!)
(def http
(node/require "http"))
(defn load [ch url]
(let [result (atom "")]
(.get http url (fn [response]
(.setEncoding response "utf8")
(.on response "data" (fn [data] (swap! result str data)))
(.on response "end" (fn [] (put! ch {:url url :content
@result})))))))
(def needed-answers 25)
(def results (async/chan))
(def urls ["http://news.ycombinator.com"
"http://google.de"
"http://nodejs.org"
"http://stackoverflow.com"])
(defn load-all []
(go-loop [answers [] answered 1]
(let [answer (<! results)]
(if (= answered needed-answers)
(conj answers answer)
(recur (conj answers answer) (inc answered))))))
(defn -main []
(doseq [url urls]
(load results url))
(go
(let [answers (<! (load-all))]
(doseq [a answers] (println (:url a))))))
(set! *main-cli-fn* -main)
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.