funcool

A channel for discussing and asking questions about Funcool libraries https://github.com/funcool/
defa 2021-03-03T07:43:27.039900Z

Hi, I’m trying to use promesa in a ClojureScript project targeting React Native but I have some issues where documentation does not help. On the JVM the following core works fine, but when executing in ClojureScript on device I get an error:

(require '[promesa.core :as p])
=> nil
(p/resolved? (p/promise 42))
=> #object[Error Error: No protocol method IState.-resolved? defined for type object: [object Object]]

defa 2021-03-03T09:31:51.041500Z

Thanks@mccraigmccraig … oh, right. Of course… can not work in JavaScript. Stupid me 🙈. My current solution is what you suggested with the atom… works fine.

mccraigmccraig 2021-03-03T09:33:58.041900Z

👍

defa 2021-03-03T07:45:03.040300Z

However the following does work:

(p/map println (p/promise 42))
42
=> #<Promise[~]>

defa 2021-03-03T07:46:40.040500Z

But what about

@(p/promise 42)
=> #object[Error Error: No protocol method IDeref.-deref defined for type object: [object Object]]
The documentation says the @ reader macro does only work on the JVM, but deref is not working, too:
(deref (p/promise 42))
=> #object[Error Error: No protocol method IDeref.-deref defined for type object: [object Object]]

defa 2021-03-03T07:48:16.040700Z

When developing in the REPL I’d like to wait synchronously a promise to return its value…

mccraigmccraig 2021-03-03T07:52:04.040900Z

@ is just sugar for deref - neither will work in cljs because there is only a single thread in javascript, and if you block it the world stops

mccraigmccraig 2021-03-03T07:57:27.041100Z

you can do something like

(defn grab [p] 
  (let [v-a (atom nil)] 
    (p/handle p 
      (fn [s f] 
        (reset! v-a [s f]))) 
    v-a))

mccraigmccraig 2021-03-03T07:58:01.041300Z

then deref the atom