This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-12
Channels
- # announcements (4)
- # babashka (60)
- # beginners (3)
- # calva (15)
- # cherry (1)
- # clojars (9)
- # clojure (30)
- # clojure-denmark (5)
- # clojure-europe (23)
- # clojure-losangeles (1)
- # clojure-norway (8)
- # clojurescript (49)
- # core-async (3)
- # cursive (15)
- # datomic (4)
- # deps-new (1)
- # emacs (36)
- # figwheel-main (3)
- # fulcro (16)
- # girouette (2)
- # hyperfiddle (1)
- # introduce-yourself (3)
- # lambdaisland (6)
- # nrepl (23)
- # off-topic (7)
- # pedestal (10)
- # polylith (50)
- # practicalli (1)
- # releases (2)
- # sci (16)
- # shadow-cljs (27)
- # slack-help (3)
- # sql (17)
- # tools-deps (5)
Is deref
not supported for Promesa promises?
(println (deref (p/promise 1)))
ERROR: No protocol method IDeref.-deref defined for type object: [object Promise]
{:type :sci/error, :line 8, :column 16, :message "No protocol method IDeref.-deref defined for type object: [object Promise]"
Are the Promesa macros (`p/then` , etc) the only way to operate on the value?
(Iām evaling this via Joyride, if that context is useful)
@U08TWB99B indeed, deref is not supported for promesa promises
You could wrap promises with a deref implementation but it'll never be realized in the same synchronous context like your doing there.
If it's useful for you to be able to deref the promise in a later call, you could do something like this:
(extend-type js/Promise
ICloneable
(-clone [p] (.then p)))
(defn derefable-promise [v]
(let [p (js/Promise.resolve v)
resolved? (atom false)
resolved-value (atom nil)
res-p (-> p
(.then (fn [result]
(reset! resolved? true)
(reset! resolved-value result)
result)))]
(specify
res-p
IPending
(-realized? [_] @resolved?)
IDeref
(-deref
[_]
(if-let [res @resolved-value]
res
res-p)))))
So in a do block, you couldn't print out the value in a blocking way:
(do
(def p-fetch (js/fetch ""))
(def p-json (.then p-fetch #(.json %)))
(def p-iss (.then p-json
#(when-let [iss-position (.-iss_position %)]
(println :iss-position iss-position)
(js->clj iss-position :keywordize-keys true))))
(def derefable-iss
(derefable-promise p-iss))
(println :derefed-iss @derefable-iss))
:derefed-iss #object[Promise [object Promise]]
;=> nil
But you could then later deref it:
@derefable-iss
:iss-position #js {:longitude -48.2400, :latitude 4.6248}
;=> {:longitude "-48.2400", :latitude "4.6248"}
@U04V15CAJ are you worried about polluting promise for other callers in the environment?