core-async

2019-09-19T11:26:37.016200Z

Guys how is it possible to save the value returned by a go block into a variable?

2019-09-19T12:39:04.016700Z

doesn't go just return a channel, so can't you just use def or something?

jumar 2019-09-19T12:57:42.017300Z

@abhi18av The question is why do you need that and what do you mean by a "variable"

2019-09-23T12:28:39.020600Z

Ohh, I think I always confused var and variables - thanks for this hint @cjsauer šŸ˜… Iā€™d definitely go with moving the -> inside the def since itā€™s seems more idiomatic

2019-09-19T12:59:27.017400Z

@jumar, a def expression to be precise

jumar 2019-09-19T13:00:33.017600Z

and why do you want to save the go return value into a global var?

2019-09-19T13:17:03.017800Z

Yup I tried, but it stores the channel not the value it returns

2019-09-19T13:28:47.018Z

Hmm, good question! Let me give more context here

(defn api [{::keys [endpoint method token]
            :or    {method :get}}]
  (->
    (go
      (&lt;? (fetch/request-async {#_#_::http/url "<https://api.spotify.com/v1/artists/3WrFJ7ztbogyGnTHbHJFl2>"
                                ::http/url     (str "<https://api.spotify.com/v1/>" 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 šŸ™‚

2019-09-19T13:36:25.018200Z

@sogaiu, hereā€™s the context

jumar 2019-09-19T13:42:46.018600Z

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

2019-09-19T13:47:35.018900Z

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

2019-09-19T13:48:16.019200Z

Maybe thereā€™s a better way to get this done :thinking_face:

cjsauer 2019-09-19T20:53:05.019400Z

@abhi18av late reply, but maybe you just need to move your thread -&gt; inside the go block:

(defn api [{::keys [endpoint method token]
            :or    {method :get}}]
  (go
    (-&gt;
     (&lt;? (fetch/request-async {#_#_::http/url "<https://api.spotify.com/v1/artists/3WrFJ7ztbogyGnTHbHJFl2>"
                               ::http/url     (str "<https://api.spotify.com/v1/>" endpoint)
                               ::http/headers {:authorization (str "Bearer " token)}
                               ::http/as      ::http/json
                               ::http/method  "get"})))
    :body))

cjsauer 2019-09-19T20:54:24.019600Z

And then if you really wanted to store this in a var, you would do this: (go (def result (&lt;! (api ...))))

cjsauer 2019-09-19T20:55:24.020Z

This is really questionable Clojure thoā€¦really only useful while debugging at the REPL.

cjsauer 2019-09-19T21:04:23.020200Z

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 āš ļø