Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

I follow this article to play with cljs, but wonder how to wait go block in the top level, the problem arise when eval go block in REPL, which return #object[cljs.core.async.impl.channels.ManyToManyChannel].

After compiled to file, and exec via node target/main.js, it seems top level go block get blocked, waiting for its execution.

I suspect the difference between those two execution ways is related to how go is implement, or how event loop. works, but can't be sure.

(go ;; how to wait this ??
  (let [browser (<! (u/new-browser))
        page (<p! (.newPage browser))]
      (<p! (.goto page "https://clojure.org"))
      (<p! (.screenshot page #js{:path "./target/screenshot.png"}))
      (catch js/Error err
        (println (ex-cause err)))
      (finally (<p! (.close browser))))))  ;; eval here return ManyToManyChannel

(defn new-browser [& {:as opts}]
  (let [pp-opts (-> opts
        (update :user-data-dir #(or % (str home-dir "/pp")))
        (clj->js :keyword-fn csk/->camelCase))]
    (go (<p! (.launch puppeteer pp-opts)))))

1 Answer

0 votes

After some research, I can answer this question myself.

Becase there is no blocking API in JS, we have to consume a chan inside a go block, so there is no way to read the uppermost go block.