Let's suppose that you have a sync API send
(send params)
This API has at least 2 behaviors:
- Return a value
- Throw an exception
In our first atempt, we will add 1 extra parameter
(send-async params ex-handler)
This function now can:
- Thow a sync exception (before go async)
- Return a value, that is a
promise
- Call
ex-handler
with the async exception. The return ofex-handler
will be delivered into thepromise
- Close the channel, even if
ex-handler
returnsnil
(that can't be inserted on a chan)
Is a core.async
good pratice to allow the caller to pass it's return the channel
(send-async params ch ex-handler)
This function should still return ch
Sometimes the caller want full control over the channel, so you should allow the caller to control if you should close the channel or not.
(send-async params ch close? ex-handler)
Let's recap
send-async
can throw before go asyncch
is a optinal parameter, will always be returned by the function. On success, the result will be inserted and the channel closed.close?
will control if the channel will be closed or notex-handler
is a function that receives an exception and returns a value. if it's value is notnil
, it should be inserted onch
https://github.com/Datomic/mbrainz-importer/blob/master/src/cognitect/xform/async_edn.clj