r/Clojurescript • u/_woj_ • Dec 11 '17
Core.Async Is Hard & Confusing
I have a background in the JavaScript world, and I'm used to making http request with APIs like fetch, promises, and observables. Coming to ClojureScript I was dumbfounded when I saw core.async syntax, and to be honest I still don't fully understand it. All these cryptic symbols and weird gotchas. It seems extremely complected and overengineered which is odd to me given that simplicity is one of Clojure's core values.
6
Upvotes
View all comments
Show parent comment
3
u/bostonou Dec 12 '17 edited Dec 12 '17
It sounds like there's a common mistake that people make with Clojure that you may be making now. "Why do I need this new, difficult thing to do this easy thing?" For a simple GET request, sure channels are overly complicated. But when you have thousands of lines of JS, with many callbacks and async events, a design to manage that complexity is necessary.
Some general ways I think about channels in ClojureScript (forgive the mixing of cljs and js syntax please):
JS runs in a single event loop (basically). Every single event is handled in this event loop.
That click event is handled in the same loop as every other event. It'd be simpler if we could have an event loop that was dedicated to each event.
Now, we can set up event loops for each specific event, and we can do it at any granularity we want. Channels are low-level tools that let us build what we need.
Additionally, it's helpful to think of channels are queues, which let us do several things pretty easily.
For example, if you want to handle mouse move events, it's normal to only care about the latest event instead of trying to process all of the events.
Now, whoever pulls events off
chanwill only get the latest event. Any old events on the queue will be dropped as soon as a new event comes in.There are more use cases, especially when in the multi-threaded world of clojure. But core.async is one of the most useful libraries in cljs IMO.
Getting back to the meta-comment I made at the beginning, I regularly see people discount something because it's usefulness is not apparent for the complexity of a e.g. todo app. When you hit a 20k LOC app, the usefulness becomes much clearer.
Here's a post I wrote about implementing
Promise.allwith core.async that may be helpful. It shows how something that we're used to having to polyfill can be built with the right building blocks.