Core.async Code Styles


#1

From an article on Lispcast:

  1. Use callback style, if possible

    You want your functions universally usable (not just by core.async code).

  2. <<< “lifter” function

    If you always put the callback as the last argument, you can use this function to convert callback style to core.async return-a-channel style.

  3. < prefix

    Prefix functions that return a channel with a < because it visually indicates how it should be called (usually using <!).

Do you have any coding styles that help you along?


#2

i love the lifter function (<<<) and i am using it in my code.
Do you have some best practices when it comes to error handling with core.async?
For example, i send an ajax request with (<<<my-ajax-fn “http://xxx”) and it will result in an error.


#3

Hi Thomas,

There are a couple of approaches.

One is to pass the entire response, regardless of success or error, down the channel, and let the receiver handle it. The receiver can tell if it’s a 2xx response and deal with it appropriately.

The other option is if it’s something that throws an exception, there’s the possibility of wrapping your code in a couple of macros that catch errors in the go block and rethrow them when you take from the channel. Here’s an example of that: http://martintrojer.github.io/clojure/2014/03/09/working-with-coreasync-exceptions-in-go-blocks/

I hope that helps.

Eric