Fork me on GitHub
#core-async
<
2016-07-21
>
gowder03:07:17

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...

danielcompton03:07:21

@gowder: b64 is going to be assigned the go channel

danielcompton03:07:52

you could simplify this to

(make-req url)

danielcompton04:07:36

make-req is presumably going to return a channel

danielcompton04:07:49

so you can just park on that channel

gowder04:07:08

@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

danielcompton04:07:34

do you want fetch-binary to block until the results come back?

danielcompton04:07:34

(defn fetch-binary [url]
  (<!! (make-req url)))

danielcompton04:07:49

use a blocking take

danielcompton04:07:58

This is in CLJ right?

gowder04:07:36

that's part of what's confusing me. I could probably handle jvm threads...

danielcompton04:07:42

you can’t block in JS

gowder04:07:46

but I'm kind of a js newb

danielcompton04:07:11

so you’re going to need to do all of the work inside a go block or use callbacks

gowder04:07:57

so it really should be something like

(defn fetch-binary [url]
  (go (<! (make-req url))))
?

gowder04:07:22

(this will be the second time I've ever used core.async in cljs ever...)

danielcompton04:07:10

that is equivalent to just returning (make-req url)

danielcompton04:07:28

the go macro will return a channel which your consuming code will need to park on

danielcompton04:07:44

so you may as well return the channel from make-req

danielcompton04:07:55

what does the code calling fetch-binary look like?

gowder04:07:02

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...)

gowder04:07:49

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.

gowder04:07:52

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. 🙂

darwin04:07:05

(func b64)

gowder04:07:30

haha oops yeah @darwin. that's what happens when I start to think about async code and browsers... my brain stops being able to handle things like remembering what symbol is what.