Fork me on GitHub
#cljsrn
<
2017-01-19
>
ejelome21:01:50

thanks @petterik 🙂

ejelome21:01:10

... wasn't able to play with cljs yesterday (so tired)

ejelome21:01:03

btw, I've moved to cljs.http and I think I understand its flow, e.g.: 1. cljs-http request returns a core.async channel (don't know what a channel is, but I'm thinking it's a response object) 2. that channel contains a success or failed response (still thinking this is a JS object, same what you receive if you do AJAX in JS) 3. we can read that channel response using the <! keyword that must be inside a go block ... so here's my attempt, however, I get a blank page:

[text (str (:status (go (<! (http/get "")))))])

artemyarulin21:01:09

nope, it wouldn’t work as essentially it will be like [text (str (:status ChannelObject#2123)))] Inside go macro you can do blocking and so on, but for outside code go block will be evaluated into new channel object

artemyarulin21:01:19

Can you use anything simpler but core.async? It’s nice but overkill for making just HTTP calls and requires some learning. But actually in any case you have to invert logic - do async HTTP IO and once it’s done do (re) rendering

ejelome21:01:03

thanks @artemyarulin, tbh, I don't even know what to use, someone suggested this so I tried it, but since it uses core.async, it brought me another layer that I basically don't even understand just to fetch a json data from a web server

ejelome21:01:34

ok, this one returns an [Object] [Object]:

[text (str (go (let [response (<! (http/get ""))]
                   (:status response)
                   (map :login (:body response)))))])

beppu21:01:24

@ejelome This isn't the best option, but maybe to get something temporarily working, you could use a blocking take <!!.

[text (str (:status (<!! (http/get ""))))]

ejelome21:01:45

what's the double bang?

artemyarulin21:01:50

there is no blocking read in CLJS as far as I know

artemyarulin21:01:06

because there is no blocking IO in JS 🙂

ejelome21:01:07

oh well, probably too many constraint for a simple http get

beppu21:01:21

@ejelome The double bang implies that it's a blocking operation that can be used outside of a go block. https://clojuredocs.org/clojure.core.async

artemyarulin21:01:35

I guess the best way just use XmlHttpRequest

artemyarulin21:01:30

Here some wrapper that I wrote some time ago, or just do it manually https://github.com/artemyarulin/koh/blob/master/http/transport_client.cljs#L31-L47

artemyarulin21:01:03

or wrap fetch which is also supported on RN evironments. But again in any case you need to reverse logic - do IO and in callback do render

ejelome21:01:51

that's a great sample, thanks!

ejelome23:01:48

oh well, last attempt with fetch but I still failed, will try again later 😞

(defn app-root []
  [text (.-title
         (-> (js/fetch "")
             ;; (clj->js {:method "GET"}))
             (.then #(.json %))
             (.then js->clj)
             ;; (.then success-handler)
             ;; (.catch rror-handler)
             ))])

ejelome23:01:24

thanks btw @artemyarulin and @beppu, learned new things today 🙂