clojure.async:"<!not in(go ...)block"错误

use*_*487 3 clojure core.async

当我评估以下core.async clojurescript代码时,我收到一个错误:"Uncaught Error:<!used not in(go ...)block"

(let [chans [(chan)]]
  (go
   (doall (for [c chans]
     (let [x (<! c)]
       x)))))
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?它绝对看起来像<!是在阻止.

Art*_*ldt 5

因为go块不能跨越函数边界,所以在很多这些情况下,我倾向于回退到loop/recur.该(go (loop模式非常常见,它在core.async中有一个简写形式,在以下情况下很有用:

user> (require '[clojure.core.async :as async])
user> (async/<!! (let [chans [(async/chan) (async/chan) (async/chan)]]
                   (doseq [c chans]
                     (async/go (async/>! c 42)))
                   (async/go-loop [[f & r] chans result []]
                     (if f
                       (recur r (conj result (async/<! f)))
                       result))))
[42 42 42]
Run Code Online (Sandbox Code Playgroud)