core.async - Clojurescript - Uncaught Error: <! used not in (go ...) block -
i in clojurescript, , trying use core.async
result native javascript function (i in browser), , conditionally integrate map.
i have function wrap native browser call (inspired timothy baldridge's talk on core.async, approx. 34 min interested) :
(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 not supported"})) c))
i can use :
;; works (go (.log js/console "res1 -" (<! (geolocation)))) ;; (not sure how print though) (go (println "res2 - " (-> {} (assoc :test (<! (geolocation)))))) ;; fails... why ? (go (println "res3 - " (-> {} (#(when true (assoc % :test (<! (geolocation))))))))
the last example fails error : uncaught error: <! used not in (go ...) block
, though thought using same kind of structure.
what doing wrong ?
note : requiring :require-macro
directive, like described here.
the go
macro take body it's given , transform <!
, alt!
, >!
calls state machine. won't walk functions:
couldn't use loop in go block of core.async?
by stops translation @ function boundaries, mean this: go block takes body , translates state-machine. each call
<!
>!
oralts!
(and few others) considered state machine transitions execution of block can pause. @ each of points machine turned callback , attached channel. when macro reaches fn form stops translating. can make calls<!
inside go block, not inside function inside code block.this part of magic of
core.async
. withoutgo
macro, core.async code lot callback-hell in other langauges.
Comments
Post a Comment