This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-21
Channels
- # announcements (1)
- # babashka (39)
- # beginners (91)
- # cider (9)
- # clj-kondo (10)
- # cljsrn (1)
- # clojure (54)
- # clojure-europe (45)
- # clojure-italy (2)
- # clojure-nl (1)
- # clojure-spec (39)
- # clojure-uk (21)
- # clojurescript (7)
- # core-typed (1)
- # cursive (9)
- # data-science (1)
- # datomic (2)
- # docker (3)
- # emacs (11)
- # figwheel-main (11)
- # fulcro (19)
- # java (1)
- # juxt (1)
- # kaocha (68)
- # malli (7)
- # meander (5)
- # off-topic (76)
- # pedestal (1)
- # re-frame (6)
- # reveal (1)
- # rum (2)
- # shadow-cljs (48)
- # sql (6)
- # tools-deps (47)
- # vim (8)
- # xtdb (23)
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 https://wttr.in 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 code...in JS, that function would be something like:
fetch(WTTR_URL)
.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?