Fork me on GitHub
#clojurescript
<
2023-05-28
>
Tim Malov18:05:09

Hello everyone. We are trying to build an js library using clojurescript and recently we ran into the problem, which we don't know how to solve and would really appreciate an advise: we are making http requests, and in our case, with http-cljs library. In the end, our api function returns ManyToManyChannel and we don't know how to synchronously get the result out of it in js code itself. Is it even possible? Or, if not, what would you suggest to use instead of http-cljs?

thheller18:05:54

this is not possible. every IO in JS is async. doesn't really matter what you use, its all gonna be async

jpmonettas18:05:51

if I understand correctly you are asking how to expose the ManyToManyChannel to JS code? If that is the case you can return promises (something that is easy to handle from JS) that get fulfilled when you get enough on the chan

jpmonettas19:05:27

you can do it like this :

(defn chan->promise [c]
  (js/Promise.
   (fn [resolve _]
     (async/take! c resolve))))

jpmonettas19:05:49

you can use it like this:

(def my-ch (async/chan))

(.then (chan->promise my-ch)
       (fn [v] (js/console.log "Got " v)))

(async/put! my-ch :hello)

jpmonettas19:05:10

does it make sense?