Update:
This question was asked and answered before core.async was released. core.async is designed to solve this particular problem, you should definitely use it for all projects in the future.
Original answer:
This is not truly asynchronous, but I really enjoyed using atoms and observers. Very simple, but very flexible model and built into both languages.
An extremely simple example:
(def my-channel (atom nil)) ;; subscribe (add-watch my-channel :watcher1 (fn [_ _ _ message] (js/alert (str "Received message: " message)))) ;; publish (reset! my-channel "my-message") ;; unsubscribe (remove-watch my-channel :watcher1)
The beauty of this approach is that the state of an atom can be any object. Here I just rewrite the state of the atom, but you can also have the state of the atom in the form of a complete message history or the last 5 messages or a state machine representing your entire system, or whatever you want to do.
source share