可以使用 core.async 进行绑定(bind)吗?我使用的是 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})))
最佳答案
在 ClojureScript 中1,binding
基本上是with-redefs
加上额外的检查,确保所涉及的变量被标记为 :dynamic
。另一方面,go
s 被安排以 block 的形式执行1(也就是说,它们可能被“停放”并稍后恢复,并且 go
block 之间的交错是任意的)。这些模型根本无法很好地啮合。
简而言之,不,请改用显式传递的参数。
1 Clojure 中细节有所不同,但结论是一样的。
2 使用尽可能最快的机制,setTimeout
如果没有更好的可用时间,则时间为 0。
关于clojure - 绑定(bind)函数如何与 core.async 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575866/