This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-19
Channels
- # announcements (1)
- # beginners (115)
- # calva (7)
- # cider (8)
- # clj-kondo (3)
- # cljdoc (12)
- # clojure (50)
- # clojure-europe (4)
- # clojure-italy (5)
- # clojure-nl (6)
- # clojure-spec (70)
- # clojure-uk (88)
- # clojurescript (54)
- # core-async (16)
- # cursive (5)
- # datomic (31)
- # editors (4)
- # emacs (4)
- # fulcro (29)
- # graphql (17)
- # luminus (1)
- # lumo (2)
- # off-topic (37)
- # pathom (16)
- # random (2)
- # re-frame (5)
- # reitit (3)
- # rum (2)
- # shadow-cljs (192)
- # sql (11)
Guys how is it possible to save the value returned by a go
block into a variable?
Yup I tried, but it stores the channel not the value it returns
@U06BE1L6T, a def
expression to be precise
Hmm, good question! Let me give more context here
(defn api [{::keys [endpoint method token]
:or {method :get}}]
(->
(go
(<? (fetch/request-async {#_#_::http/url ""
::http/url (str " " endpoint)
::http/headers {:authorization (str "Bearer " token)}
::http/as ::http/json
::http/method "get"}))
:body)))
I’m trying to only store the value of :body
in the response map in this pathom driven query 🙂@UG1C3AD5Z, here’s the context
Why do you need async HTTP request execution? Seems to me that you want to block until the response is available and return the data to the client anyway? Moreover, this kind of blocking IO isn't really suitable for go blocks
I’m operating in the cljs
environment with the pathom https://cljdoc.org/d/com.wsscode/pathom/2.2.24/api/com.wsscode.pathom.diplomat.http.fetch
Maybe there’s a better way to get this done :thinking_face:
@abhi18av late reply, but maybe you just need to move your thread ->
inside the go block:
(defn api [{::keys [endpoint method token]
:or {method :get}}]
(go
(->
(<? (fetch/request-async {#_#_::http/url ""
::http/url (str " " endpoint)
::http/headers {:authorization (str "Bearer " token)}
::http/as ::http/json
::http/method "get"})))
:body))
And then if you really wanted to store this in a var, you would do this:
(go (def result (<! (api ...))))
This is really questionable Clojure tho…really only useful while debugging at the REPL.
As an aside, you shouldn’t confuse “vars” with “variables”. They are totally different things. Vars are always top-level, and using def
is not assignment like in other languages. Their fundamental purpose is to hold constant values. This is why the above code snippet comes with big warning signs
⚠️ Only do this while debugging at the REPL ⚠️
Ohh, I think I always confused var
and variables
- thanks for this hint @U6GFE9HS7 😅
I’d definitely go with moving the ->
inside the def since it’s seems more idiomatic