r/Clojurescript • u/dudleycraig • Nov 13 '21
js/Promise using core.async and not javascript chaining?
I'm trying to put an async result onto a channel, and retrieve the result via a ratom, in javascript it's something like ...
const querySnapshot = await getDocs(collection(db, "users"));
I've googled and haven't really found anything that definitively works ... can someone show a low level, native, core.async method/methods of achieving this? via chan, go, go-loop, <!, >!, put!, etc?
3
Upvotes
2
u/dudleycraig Nov 13 '21 edited Nov 13 '21
after a bit of searching around, the answer turned out to be rather trivial ... which is precisely what i wanted ...async request via channels instead of chaining promises ...
(def companies (reagent/atom []))request ...
(defn request-companies [](try(async/go(<p! (getDocs (collection (getFirestore) "companies"))))(catch js/Object error (.error js/console "Failed retrieving companies, " error))))handler ...
(defn handle-companies [channel](async/go(let [response (async/<! channel)results (get-results response)](reset! companies results))))and usage ...
(handle-companies (companies-service/request-companies))2
u/pihkal Nov 13 '21
It’s been a while since I looked into this, but I think you’re on the right track with the p->c and <p! macros.
The Promesa library might also be useful.
1
u/p-himik Nov 13 '21
Sounds like
core.asyncwill actually complicate things for you. If you want to put the results into a ratom, why not just(.then promise #(reset! my-ratom %))?