英文版本有个文章 http://ku1ik.com/2015/10/12/sweet-core-async.html
感觉中间的代码片段很实用, 抄一份~
ClojureScript 当中 Macro 没有 Clojure 那么直接, 所以 macros 是需要单独引用的:
(ns demo.core
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [cljs.core.async :refer [put! chan <! >! timeout close!]]))
go-loop
函数用来循环监听 channel 当中的数据:
(let [data-chan (coll->chan frames)]
(go-loop []
(when-let [text (<! data-chan)]
(print text)
(recur))))
alts!
函数接收 vector 包含的两个 channel, 结果拿到一个 value 和对应的 channel 形成的 Vector:
(let [t (timeout msec)
[_ c] (alts! [input t])]
(when (= c input)
(recur))))
其他的 go
timeout
>!
<!
倒是蛮好记的. 忘了 put!
什么场合用的…