Kayshap,

Just a couple of notes, which presume you've a good reason for inventing
another bridge between browser-native apis and core.async:
 - Contrast https://clojure.github.io/core.async/#clojure.core.async/put!
w/ your use of (go (>! x))
 - In general, functions which place values onto channels may benefit from
optionally accepting channels (`[& {:keys [chan] :or {chan (async/chan}]`),
- or whatever - doing the defaulting in a let binding is probably more
readable.  In the case of get-token, it could e.g. pass in a channel w/ an
map xform (see async/chan's docs).

There's a lot of redundancy in this function:

(defn get-message [endpoint token]
  (let [input-chan (do-request endpoint "GET" "" {"Authorization" (str
"Bearer: " token)})
        output-chan (chan)
        _ (go (let [input-result (<! input-chan)]
                (>! output-chan input-result)))]
       output-chan))

Note that 'go' creates a channel, containing the value the body of the
value the go block evaluates to.  Let's try shrinking the function a little:

(defn get-message [endpoint token]
  (let [input-chan (do-request endpoint "GET" "" {"Authorization" (str
"Bearer: " token)})]
    (go
     (let [input-result (<! input-chan)]
       input-result)))

As 'do-request' already returns a channel, and we don't want to transform
its value/s, we can shrink the function further:

(defn get-message [endpoint token]
  (do-request endpoint "GET" "" {"Authorization" (str "Bearer: " token)}))

This generalizes to many of the other functions, in which there is apparent
confusion about what 'go' does, and why and when channels ought to be
created.  The core.async API docs & writeup are pretty good, and I think
it's likely there's some relevant content on YouTube.

- 'f' isn't accomplishing anything in do-poll, and async/go-loop and
async/timeout may be more readable ways of expressing do-poll's dynamics.
- There's a few weird things that don't relate to core.async, like binding
expressions to "_" within (let), which - while helpful for print statements
during debugging - is difficult to justify in other context.

Take care,
Moe


On Tue, Nov 13, 2018 at 3:10 PM Kashyap CK <ckkash...@gmail.com> wrote:

> [Apologies for cross-posting - but I realize that Clojurescript group may
> be a better place for this query than the Clojure group :) ]
>
> Hi all,
>
>
> I am attempting to use core.async to poll a service -
> https://gist.github.com/ckkashyap/c8423dcfc3a3f28b67e18ae76cc13f53
>
> Broadly, I need to hit the service endpoint with a secret to get a token
> and subsequently use the token to poll for messages.
>
>
> I'd appreciate any feedback on the approach - particularly around
>
> 1. Error handling
>
> 2. Use of channels
>
>
> Is there some project out there I could take a look at to learn the
> right/good way to do this?
>
>
> Regards,
>
> Kashyap
>
> --
> Note that posts from new members are moderated - please be patient with
> your first post.
> ---
> You received this message because you are subscribed to the Google Groups
> "ClojureScript" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojurescript+unsubscr...@googlegroups.com.
> To post to this group, send email to clojurescript@googlegroups.com.
> Visit this group at https://groups.google.com/group/clojurescript.
>

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.

Reply via email to