Stumbling Through Interop
》Surely I can just cargo cult this
After making so much process building a
progressive web app, I decided to take a detour into making some "simpler"
pages, without re-frame. To pull in some EDN data to work with, I quickly
needed to figure out how to use fetch. JavaScript interop looks like Java
interop, right? Objects, members, methods. What works in one place should
work in the other. Right?
》Enter promises and consternation
I started playing around just by seeing what fetch returns:
(js/fetch edn-url ) ; => #object[Promise [object Promise]]Getting a promise object in return was not a surprise. Previous work taught
me that JS uses async code and promises heavily. Which makes sense — any
blocking of a page’s JS thread leads to a frozen page. Treating the promise
like a magic bottlecap, I reach for the handy old tools realized? and deref:
(realized? (js/fetch edn-url )); => :repl/exception!
(deref (js/fetch edn-url )); => :repl/exception!Oh, no! This isn’t the same at all.
From Clojure to ClojureScript
》A Journey
How do you get from Clojure to ClojureScript? Though I have had much practice with Clojure and understand the ecosystem around it, understanding the environment of ClojureScript has remained elusive. Surely it is possible to use ClojureScript without having a deep understanding of JavaScript, just like most Clojure code can be written with minimal Java knowledge?
I had dabbled in some JavaScript before — enough to know that I would rather be writing ClojureScript if at all possible. But most of the advice given to me was along the lines of: "go learn JavaScript React and come back later." Almost as if the only people coming to ClojureScript were JavaScript developers!
Here is what I’ve learned as I struggled to put a web app together. I hope it helps other developers who might be coming from a similar background.