Yes, this is fine.

It is slightly more idiomatic to use volatile! (introduced in Clojure 1.7 and 
recent ClojureScripts) if you just need local mutation and don't intend on 
sharing the atom:


(defn load-url-async 
  [url success error] 
  (let [loader (.get http url (fn [res] 
                                (let [buffer (volatile! "")] 
                                  (.on res "data" #(vswap! buffer str %)) 
                                  (.on res "end" #(success @buffer)))] 
    (.on loader "error" error)) 

On Monday, June 22, 2015 at 11:06:32 AM UTC-5, Honza Břečka wrote:
> Hi, I'm not sure if I can rewrite the following piece of JavaScript targeting 
> node:
> 
> var loadUrl = function(url, success, error) {
>   http.get(url, function(res) {
>     var buffer = '';
>     res.on('data', function(chunk) {
>       buffer += chunk;
>     });
>     res.on('end', function() {
>       success(buffer);
>     });
>   }).on('error', function(e) {
>     error(e);
>   });
> };
> 
> to this:
> 
> (defn load-url-async
>   [url success error]
>   (let [loader (.get http url (fn [res]
>                                 (let [buffer (atom "")]
>                                   (.on res "data" #(swap! buffer str %))
>                                   (.on res "end" #(success @buffer)))]
>     (.on loader "error" error))
> 
> Can someone experienced confirm that I can use atom inside the function?
> 
> Thanks

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to