【发布时间】:2015-08-16 17:09:55
【问题描述】:
我在 Clojurescript 中,并尝试使用 core.async 从原生 Javascript 函数(我在浏览器中)获取结果,并有条件地将其集成到地图中。
我有一个封装原生浏览器调用的函数(受Timothy Baldridge's talk on core.async 启发,感兴趣的人大约需要 34 分钟):
(defn geolocation []
(let [c (chan)
cb (fn [e] (put! c (js->clj e)))]
(if (exists? js/navigator.geolocation)
(.getCurrentPosition js/navigator.geolocation. cb cb)
(put! c {:error "Geolocation is not supported"}))
c))
我可以这样使用它:
;; this works
(go (.log js/console "RES1 -" (<! (geolocation))))
;; this too (not sure how to print it though)
(go (println "RES2 - " (-> {}
(assoc :test (<! (geolocation))))))
;; this fails... why ?
(go (println "RES3 - " (-> {}
(#(when true
(assoc % :test (<! (geolocation))))))))
最后一个示例失败并出现错误:Uncaught Error: <! used not in (go ...) block,尽管我认为我使用的是相同类型的结构。
我做错了什么?
注意:我从:require-macro 指令like described here 正确要求它。
【问题讨论】: