大問題!
我想你的第一個步驟,瞭解好處是Timothy Baldridge Video
而且我下面的嘗試:
我認爲差異將清理,如果我們改了一下你的代碼。
首先,刻意突出了一句「總會有一個在所有的好節目的時候,組件或子系統必須停止直接相互通信」從core.async的貼在clojure blog呈現提取。我認爲,我們可以輸入事件通道從設FN分開:
(def clicks (listen (dom/getElement "search1") "click"))
(go
(while true
(dosomethingasync (<! clicks))))
(put! clicks "this channel can be written from differents parts of your code")
其次,core.async我們可以寫爲我們寫同步調用(順序代碼)異步調用。這種情況的一個例子需要一個以上的頻道:
(def clicks (listen (dom/getElement "search1") "click"))
(def keys-pressed (listen (dom/getElement "search1") "keypress"))
(def to-out (chan))
(go
(while true
(let [click-recieved (<! clicks)]
(dosomethingasync click-recieved)
(>! to-out "click recieved!")
(let [other-input-waited (<! keys-pressed)]
(dosomethingwithkey other-input-waited)
(>! to-out "keypressed recieved!")
)
)
))
最後,我認爲你沒有正確使用回調函數的含義。當我們討論一個回調函數時,我認爲我們所指的是一個函數,除了它自己的參數之外,它還會接收一個函數「callback」。在函數執行結束時,我們調用回調函數將執行流程返回到原始點。 改變你的「回調」功能上來如下:
(defn dosomethingcallback
[e call-back-fn]
(js/console.log "callback: " e)
(call-back-fn))
如果我們試圖發出類似於前面的代碼core.async例如實現相同的行爲的內容:
(defn do-key
[call-back-fn e]
(.log js/console "before callback key")
(call-back-fn e))
(defn do-click
[call-back-fn e]
(.log js/console "before callback click")
(call-back-fn e))
(defn key-callback-fn [e]
(.log js/console (str "doing some work with this key: " e))
)
(defn click-callback-fn [e]
(.log js/console (str "doing some work with this click" e))
(events/listen (dom/getElement "search2") "keypress" (partial do-key key-callback-fn)))
(events/listen (dom/getElement "search2") "click" (partial do-click click-callback-fn))
其他很好的資源! http://www.infoq.com/interviews/baldridge-core-async – tangrammer