Fork me on GitHub

Hi! I was wondering if there was a recommended way, when writing a node-targeted clojurescript app, to set the results of a json fetch to a variable? I have a static page generator written in clojure, that pulls in weather data from and uses it to adjust the page in various ways. In clojure, I used clj-http.client and it was as simple as:

(def weather-info (:weather (parse-string (:body (client/get WTTR-JSON)) true)))
I am trying the same in clojurescript and getting stuck. the above library won't work, I tried cljs-http...but I think it is meant for the browser. Same with js/fetch , which is mentioned in the clojurescript docs. I'm trying to do it now using the npm module node-fetch but having a super hard time wrapping my head around translating the promise format of node-fetch into clojure. If anyone has done this, i'd be super happy to know what works for you! Thank you!


@webmaster601 unlike in Clojure, in CLJS you can’t typically block execution while doing a network request. What you can do is use something like node-fetch or some other nodejs lib to do the network request, and it will use either a promise or callback to signal when the request is complete. In the callback, you can then continue the rest of the static site generation


It will probably require some significant restructuring of your program if it’s built with the assumption that you can just refer to the weather data in a global variable


ah, that makes sense. So basically (pseudocode)

(-> (fetch WTTR_URL)
    (.then #(create-page %)))


(where create-page takes weatherinfo as an argument and uses it to run through all the page generation?


I think this is the other area where i'm a bit stuck, which is the transposing of JS, that function would be something like:

.then(res => res.json())
.then(json => createPage(json))
what would the res.json() portion look like in clojure? and would it be using the (.then #(some-function %) format i used above?


Yeah. I’m on mobile but calling methods is basically (.method object)