Fork me on GitHub
#funcool
<
2021-03-03
>
defa07:03:27

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]]

defa07:03:03

However the following does work:

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

defa07:03:40

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]]

defa07:03:16

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

mccraigmccraig07:03:04

@ 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

mccraigmccraig07:03:27

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))

mccraigmccraig07:03:01

then deref the atom

defa09:03:51

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