clojure - 绑定(bind)函数如何与 core.async 一起使用?

标签 clojure clojurescript

可以使用 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 中1binding基本上是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/

相关文章:

Clojure + Clojurescript : Macro to read code of current file

clojurescript:触摸事件和 Domina

clojure - clojure 中的函数定义宏

function - Clojure中的除数函数

scala - Clojure future 的失败

unit-testing - 在单独的框架中即时对 Clojure 进行单元测试

ClojureScript Re-frame 订阅取消引用困境

clojure - 在 Clojure 中枚举一个序列?

clojure - clojure 的 APersistentMap 实现有什么区别

clojure - Clojure 库中的可选依赖项