Clojurescript: getting the value out of go-block

501 views
Skip to first unread message

Dmitry Suzdalev

unread,
Apr 23, 2014, 5:56:33 AM4/23/14
to clo...@googlegroups.com
Hello!

I was interested in trying to wrap some callback-hellish JS async functionality into something that would look (and work) like a sync call. 
I thought that it could be some kind of function which internally uses core.async but does not expose this.

That is instead of having some function with callback on completion

(.findSomething jsLib (fn [res] (println res)))

I would like to have a sync-variant of it, something like this

(defn findSomething []
  "returns res"
  (let [c (chan)]
    (go (!< (.findSomething jsLib (fn [res] (put! c res)))))))

(println (findSomething)) ;; => prints "res" value

It would work if go-macro returned last *value* read from the channel. But go macro returns a *channel* that contains the result. So to read it in clojurescript I have to ironically wrap go in go again.
Seems like when you started using channels there's no way out of this :)
On JVM I could just use <!!  in this situation, but it is not supported in ClojureScript.

Wanted some advice/pointers from experienced programmers as I've just started and might get something completely wrong :)
Thanks in advance,
Dmitry.

Timothy Baldridge

unread,
Apr 23, 2014, 12:53:37 PM4/23/14
to clo...@googlegroups.com
What you want is not possible since core.async does not do full program rewriting, only code within a go is transformed. This is for both performance and practical reasons. 

So yes, wrapping a go in a go may seem a bit pointless, but that's exactly the semantic that you want, in a single threaded environment like JS VMs it is also the only possible solution. 

If you think about it, the semantics of what you are asking for is not possible. For how would you pause the execution of the (println) until the async evaluation of its arguments had completed?

Timothy




--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Dmitry Suzdalev

unread,
Apr 23, 2014, 4:46:05 PM4/23/14
to clo...@googlegroups.com
Oh, right! Example with println clears it. 
I should gave it some more thought from the how-it-works-internally perspective!
Guess I just was blown away by the expressiveness of the syntax which makes everything look so simple and straightforward and sync-like :)

Thanks, Timothy! :)
Reply all
Reply to author
Forward
0 new messages