绑定函数如何与 core.async 一起使用?

How does the binding function work with core.async?

可以使用 core.async 绑定吗?我使用的是 ClojureScript,所以 core.async 非常不同。

(def ^:dynamic token "no-token")
(defn call 
  [path body] 
  (http-post (str host path) (merge {:headers {"X-token" token}} body)))) ; returns a core.async channel
(defn user-settings
  [req]
  (call "/api/user/settings" req))
; elsewhere after I've logged in
(let [token (async/<! (api/login {:user "me" :pass "pass"}))]
  (binding 
    [token token] 
    (user-settings {:all-settings true})))

在 ClojureScript1 中,binding 基本上是 with-redefs 加上一个额外的检查,确保所涉及的 Vars 被标记为 :dynamic。另一方面,gos 被安排以块的形式执行 1(也就是说,它们可能 "parked" 并稍后恢复,并在 [=13] 之间交错=] 块是任意的)。这些模型根本不能很好地啮合。

简而言之,不,请改用显式传递的参数。


1在Clojure中细节有所不同,但结论是一样的

2 使用尽可能最快的机制,setTimeout 如果没有更好的可用时间,则时间为 0。