This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-21
Channels
- # admin-announcements (4)
- # beginners (41)
- # boot (46)
- # cider (8)
- # clojure (132)
- # clojure-austin (15)
- # clojure-belgium (3)
- # clojure-greece (3)
- # clojure-hk (1)
- # clojure-mexico (4)
- # clojure-quebec (5)
- # clojure-russia (46)
- # clojure-spec (225)
- # clojure-taiwan (1)
- # clojure-uk (17)
- # clojurescript (46)
- # clojurewerkz (1)
- # core-async (28)
- # cursive (9)
- # datascript (3)
- # datomic (5)
- # defnpodcast (42)
- # devcards (60)
- # emacs (27)
- # hoplon (7)
- # lein-figwheel (5)
- # leiningen (12)
- # mount (8)
- # om (13)
- # play-clj (2)
- # reagent (47)
- # rethinkdb (5)
- # ring-swagger (7)
- # spacemacs (9)
- # specter (12)
- # testing (1)
- # untangled (1)
- # vim (11)
- # yada (31)
Hmm... can I bug the group with something silly? I'm pretty new to core.async, and I feel like this function is wrong, but I'm not sure why. something about the let and the go...
(defn fetch-binary [url]
(let [b64 (go (<! (make-req url)))]
b64))
(where make-req is a function that fetches some data, does conversions to it, and returns a channel)
something just smells funny with the let and the go block, like I have more code than necessary...@gowder: b64
is going to be assigned the go
channel
you could simplify this to
(make-req url)
make-req
is presumably going to return a channel
so you can just park on that channel
@danielcompton: thanks--- I thought I was confused. yeah, make-req
generates a http request and returns a promise-chan
that will receive the result of the request. with fetch-binary
I'm just trying to call make-req
and then return the contents of the channel when they show up
do you want fetch-binary
to block until the results come back?
(defn fetch-binary [url]
(<!! (make-req url)))
use a blocking take
This is in CLJ right?
you can’t block in JS
so you’re going to need to do all of the work inside a go block or use callbacks
so it really should be something like
(defn fetch-binary [url]
(go (<! (make-req url))))
?that is equivalent to just returning (make-req url)
the go
macro will return a channel which your consuming code will need to park on
so you may as well return the channel from make-req
what does the code calling fetch-binary
look like?
hrm. I'm super-confused. I'm trying to actually get the value off the channel, not the channel itself. So fetch-binary
is meant to be an entry-point called by arbitrary functions...
(The use here is that I'm trying to wrap all kinds of JS encoding horribleness with fetching binary files in browser...)
So the way I thought it would work is that make-req
generates a pure javascript interop http request, then handles all the encoding in its own callback, plunks the properly encoded data on a channel, and returns the channel. And then fetch-binary
calls make-req
, gets the channel, and pulls the properly encoded data off the channel when it's there.
hmm... I think I'm slowly starting to understand. if the go block returns the channel, then what I really need to do is pass the (presumably side-effecting) function that actually makes use of the properly encoded data into fetch-binary
, sort of like this:
(defn fetch-binary [func url]
(go
(let [b64 (<! (make-req url))]
(func url))))
... right? I hope so. 🙂