core-async 2023-10-19

is there an idiomatic way of calling sync java methods in an async way (maybe w/o even using core.async)? here’s my code, that a coworker commented might be too low level:

(defn call-slow-java-method> [params]
  (let [out  (promise-chan)
        req  (-> (CallSlowNetworkRequest.)
                 (.withParams params))]
    (put! out (-> (.InvokeRequest @java-client req)
                  (.getBody)
                  (String.)
                  (read-json)))
    out))

(defn call-site [params]
  (go
    (let [res (<! (call-slow-java-method> params))]
      (println "@@@ res:" res)
      res)))

if CPU bound, just call as is, if IO-bound (seems like your case), use a/thread https://clojure.github.io/core.async/#clojure.core.async/thread

so you mean something like this?

(defn call-slow-java-method [params]
  (let [req  (-> (CallSlowNetworkRequest.)
                 (.withParams params))]
    (-> (.InvokeRequest @java-client req)
        (.getBody)
        (String.)
        (read-json))))

(defn call-site [params]
  (go
    (let [res (<! (thread (call-slow-java-method params)))]
      (println "res:" res)
      res)))
?

✅ 1

thanks! it seems to be working

👍 1