June 2014
Intermediate to advanced
300 pages
7h 12m
English
Today we’ll see how core.async makes asynchronous IO both simpler and easier to understand. But before then, we’ll look at a feature we’ve not yet seen—handling multiple channels at a time.
So far we’ve dealt only with a single channel at a time, but there’s no reason we have to restrict ourselves to doing so. The alt! function allows us to write code that can deal with more than one channel:
| | channels.core=> (def ch1 (chan)) |
| | #'channels.core/ch1 |
| | channels.core=> (def ch2 (chan)) |
| | #'channels.core/ch2 |
| | channels.core=> (go-loop [] |
| | #_=> (alt! |
| | #_=> ch1 ([x] (println "Read" x "from channel 1")) |
| | #_=> ch2 ([x] (println "Twice" x "is" (* x 2)))) |
| | #_=> (recur)) |
| | #<ManyToManyChannel ... |
Read now
Unlock full access