Chapter 8. Asynchronous Processing
In this chapter, you will write an étude that uses core.async to do asynchronous processing. Even though the JavaScript environment is single-threaded, core.async allows you to work with anything that needs to be handled asynchronously; this is a very nice feature indeed.
Here are two examples of using core.async. In the first example, Annie and Brian are going to send each other the numbers 5 down to zero, stopping at zero, in a project named async1. You will need to add some :require and :require-macro specifications to your namespace:
(ns^:figwheel-alwaysasync1.core(:require-macros[cljs.core.async.macros:refer[gogo-loop]])(:require[cljs.core.async:refer[<!>!timeoutalts!chanclose!]]))
Then, define a channel for both Annie and Brian:
(defannie(chan))(defbrian(chan))
Annie gets two processes: one for sending messages to Brian and another for receiving messages from him:
(defnannie-send[](go(loop[n5](println"Annie:"n"-> Brian")(>!briann)(when(pos?n)(recur(decn))))))(defnannie-receive[](go-loop[](let[reply(<!brian)](println"Annie:"reply"<- Brian")(if(pos?reply)(recur)(close!annie)))))
In the annie-send function, you see the go function, which asynchronously executes its body and immediately returns to the calling function. The >! function sends data to a channel. The loop continues until n equals zero, at which point the function returns nil.
Because go and loop occur together ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access