Skip to content

Instantly share code, notes, and snippets.

@metametadata
Last active September 29, 2017 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metametadata/bb31af03c9949b3518ed2f94f3c84ce3 to your computer and use it in GitHub Desktop.
Save metametadata/bb31af03c9949b3518ed2f94f3c84ce3 to your computer and use it in GitHub Desktop.
a taste of core.async with <?
"
Naming conventions:
go - executes code which contains special blocking operations (e.g. <!), returns a channel, throws global uncaught exceptions
<! - reads from channel without any special treatment of returned error values
<? - reads from maybe-channel, if there's an error value - throws it
<?go - the same as go but \"swallows\" exceptions and returns a maybe-channel
<foo/<?foo:
1) channel/maybe-channel
2) a function returning a channel (e.g. <?fetch-items)
3) a function returning a <function
foo</foo<? - function or a macro which takes channel/maybe-channel as input (e.g. all-settle<?)
<cast<?/<?cast< - explicit casts to another channel type without cloning the underlying channel instance
"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and finally
(a/go
(start-progress)
(try
(do-smt-with-updated-xxx
(a/<? (api/<?update-xxx-on-server-asynchronously client "/edit-xxx" xxx)))
(catch js/Error e
(do-smt-on-error e))
(finally
(stop-progress))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; client API
(defprotocol ClientProtocol
(<?query [this url params] "GET request."),,,)
(defmacro query
"Can be run only inside go-blocks. Makes a query, parks and then returns the deserialized response."
[this url params]
`(:r (a/<? (app.api/<?query ~this ~url ~params))))
; Production code
; Waiting for several parallel async operations
(a/go
; all-settle<? macro runs async operations simultaneously, blocks until all operations are finished.
; If all operations succeed then returns a vector of results.
; Otherwise, throws an exception containing a list of all results, some of which will be errors.
(a/all-settle<?
[(<?fetch-items {:client client
:fetch-url "/fetch-table-items"
:fetch-params {,,,}})
; api/query is a macro
(a/<?go (do-smt-with-xxx (api/query client "/fetch-xxx" {,,,})))
(a/<?go (do-smt-with-yyy (api/query client "/fetch-yyy" {,,,})))
(a/<?go (do-smt-with-zzz (api/query client "/fetch-zzz" {,,,})))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment