This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-15
Channels
- # announcements (2)
- # babashka (41)
- # beginners (10)
- # calva (62)
- # chlorine-clover (70)
- # cider (19)
- # clara (2)
- # clj-http (1)
- # clj-kondo (1)
- # cljs-dev (3)
- # cljsrn (8)
- # clojure (168)
- # clojure-austin (1)
- # clojure-australia (2)
- # clojure-canada (1)
- # clojure-europe (15)
- # clojure-france (9)
- # clojure-nl (3)
- # clojure-serbia (5)
- # clojure-spec (14)
- # clojure-uk (8)
- # clojurescript (14)
- # community-development (30)
- # core-async (42)
- # core-typed (1)
- # datahike (2)
- # datalog (23)
- # datomic (4)
- # emacs (4)
- # figwheel-main (4)
- # fulcro (67)
- # ghostwheel (4)
- # girouette (1)
- # gorilla (7)
- # graalvm (11)
- # heroku (8)
- # integrant (42)
- # jobs (6)
- # jobs-discuss (47)
- # kaocha (7)
- # lambdaisland (2)
- # leiningen (5)
- # lsp (29)
- # off-topic (1)
- # pathom (9)
- # portal (6)
- # re-frame (5)
- # reagent (11)
- # releases (6)
- # remote-jobs (10)
- # shadow-cljs (112)
- # testing (55)
- # vrac (1)
How do you do JS async/await
in CLJS? I know about core.async
and have experience with it, but I have a very difficult time getting a vallue out of a Promise or an async channel (since they all need to be wrapped in go
blocks). I am still quite new to CLJS so I appreciate any help offered.
Hi, could this help: https://clojurescript.org/guides/promise-interop#using-promises-with-core-async It was new to me that we could use core.async for this. I have just used JS interop to handle promises 🙂
Remember not to try to do it in your render function. Once the promise resolves you want to call a function/hook or the like that updates you state
you could also take a look at some other libraries that help promise use in cljs : https://github.com/athos/kitchen-async
@hadilsabbagh18 I use core-async; Been using this approach for many years, hasn’t caused any issues: https://github.com/raspasov/alexandria-clj/blob/main/src/ss/cljs/promises.cljs (that’s all the code it takes) It’s literally copied from this library (mainly copied to avoid some outdated deps, etc since the library is not maintained atm) https://github.com/jamesmacaulay/cljs-promises/blob/master/src/cljs_promises/async.cljs
And then you can do anywhere:
(a/go
(let [[?value ?error] (<! (js/Promise.resolve 42))]
(println "?value" ?value)
(println "?error" ?error)))
;=>
; ?value 42
; ?error nil
You might want to ask about that in #clojurescript , @hadilsabbagh18 .