简体   繁体   English

Clojurescript core-async:go里面的异步条件

[英]Clojurescript core-async: Async condition inside go

I have a problem with ClojureScript Core-Async . 我有ClojureScript Core-Async的问题。 This only happens in ClojureScript and not in Clojure. 这只发生在ClojureScript中而不是Clojure中。

I have the following code: 我有以下代码:

(defn cc [x]
  (go 
    (println "cc: " x)
    x))

(defn foo [x]
  (go
    (when (and (= :ok (<! (cc x)))
               (= :ok (<! (cc :ok))))
      (print "after and"))))

(foo 1)
(foo :ok)

When calling (foo :ok) , the result is as expected - the function cc is called twice, and the console shows cc: :ok cc: :ok after and . 当调用(foo :ok) ,结果如预期的那样 - 函数cc被调用两次,控制台cc: :ok cc: :ok after and显示cc: :ok cc: :ok after and But, when running (foo 1) , the function cc is also run twice and the console shows cc: 1 cc: :ok . 但是,当运行(foo 1) ,函数cc也会运行两次,控制台显示cc: 1 cc: :ok So, even though the first condition isn't fullfilled, the second one is still checked! 因此,即使第一个条件没有满满,第二个条件仍然被检查!

This is appraently a bug with the ClojureScript core.async library. 这显然是ClojureScript core.async库的一个错误。 I opened a Jira issue for this. 我为此开了一个Jira问题

In the meantime I was able to work around this by using: 与此同时,我通过使用以下方法解决了这个问题:

(go
  (when (= :ok (<! (cc x))
    (when (= :ok (<! (cc :ok)))
      (print "after and")))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM