Fork me on GitHub
#clojurescript
<
2024-04-09
>
chromalchemy00:04:04

Can anyone give me a hint about how to convert this js async/await code to cljs + Promesa?

async function makeDefaultDocument(executionContext) {
  const app = require('photoshop').app;
  let myDoc = await app.createDocument({preset: "My Web Preset 1"});
}

await require('photoshop').core.executeAsModal(makeDefaultDocument);

chromalchemy02:04:02

With some help from Claude llm… This works for regular promise style.

(defn make-default-document [_ opts]
  (-> app 
    (.createDocument opts)
    (p/then
      (fn [my-doc]
        my-doc))))

(-> core
  (.executeAsModal
    (fn [execution-context]
      (make-default-document
        execution-context  
        #js {:name "my new document"})))
  (p/then 
    #(println "Document creation completed."))
  (p/catch 
    #(println "An error occurred:" %)))

chromalchemy03:04:24

Converted to use p/let

(defn make-new-document [opts]
  (p/let 
    [my-doc (.createDocument app opts)]
    my-doc))

(p/let
  [new-doc 
    (.executeAsModal core
      (fn [executionContext]
         (make-new-document #js {:name "my new document"})))]
  (println "Document creation completed.")
  (p/catch (fn [error]
             (println "An error occurred:" error))))
Not sure if it can get more sugary.

thheller10:04:25

if the only intent is for make-new-document to return the doc but never actually do anything with it you can skip the p/let entirely

thheller10:04:16

(defn make-new-document [opts]
  (.createDocument app opts))
is equivalent

chromalchemy15:04:09

Thanks, does simplify

chromalchemy15:04:12

I have an “named”? js object, and js->clj is not working on it. https://stackoverflow.com/questions/32467299/clojurescript-convert-arbitrary-javascript-object-to-clojure-script-map#answer-51439387 These solutions seem to get at the data. But what is best practice? I am working in Scittle, so dont necessarily have access to goog, interop libraries, etc.

chromalchemy15:04:47

Not sure why most of those properties are greyed out, dont serialize, but I can click on them in devtools with “invoke property getter” tooltip showing.

p-himik15:04:20

> But what is best practice? Well, what exactly are you trying to do?

Sam Ferrell15:04:32

I tend to avoid converting objects like that since their primary interface are their methods... usually its easier to just use it as a normal Javascript object with regular interop. Might depend on your use-case though

chromalchemy16:04:29

Just exploring an api right now. I was trying to convert the object to clj data and pretty-print it to view in repl. Maybe not really necessary? But maybe at some point I might want to operate on that data in regular clj data structures. Sorry, currently I feel ill at ease with js-interop and the lingo.

p-himik16:04:54

> Maybe not really necessary? This. :) Not everything can be converted, far from it. Also, even for CLJ data just pretty-printing it into a REPL is a very limited experience. I'd suggest tap> and things like Portal, Reval, REBL.

chromalchemy16:04:26

I’m in Scittle right now, so not sure if I have access to tap> + Portal. That would be nice. Ok, I’ll try to stick with js interop. Also with Scittle I don’t have access js-interop / js-bean helper libraries. Though @U2FRKM4TW If I remember right you strongly advised against using those anyway. I might be more at ease with Squint in terms of interop, but I’m biasing towards a fully realized repl connection right now.

p-himik16:04:33

Ah, right. Yeah, I'm not really a fan of any interop library. In that case, I myself would just stick to printing things to the JS console. Mouse navigation for deeply nested data that can show rich(er) data structures is much better than a plain-text REPL. But if you would still rather stick to a REPL, there's js-keys that can help. You can get any static key from an object via (.-the_key obj) and any dynamic key via (unchecked-get obj binding-with-string-key).

chromalchemy16:04:58

Ok will try those out. Thanks for the pointers.

p-himik16:04:12

> Not sure why most of those properties are greyed out https://stackoverflow.com/a/29827859/564509