Skip to content

Commit

Permalink
add very basic js-await macro for promise interop
Browse files Browse the repository at this point in the history
  • Loading branch information
thheller committed May 18, 2022
1 parent 8e7a796 commit 05f2cd1
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/main/shadow/cljs/modern.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,38 @@
:children [:parts]}))

(defmacro js-template [& body]
`(js-template* ~@body))
`(js-template* ~@body))

;; FIXME: use spec for parsing this. bad errors otherwise if used incorrectly
(defmacro js-await [[name thenable] & body]
(let [last-expr (last body)

[body catch]
(if (and (seq? last-expr) (= 'catch (first last-expr)))
[(butlast body) last-expr]
[body nil])]

;; FIXME: -> here will always return a promise so shouldn't be necessary to add js hint?
`(-> ~thenable
~@(when (seq body)
[`(.then (fn [~name] ~@body))])
~@(when catch
(let [[name & body] catch]
[`(.catch (fn [~name] ~@body))]
)))))


(comment
(macroexpand-1
'(js-await [{:keys [foo]} (bar)]
(do-thing foo)
(catch x (fail x))
))

(macroexpand-1
'(js-await [{:keys [foo]} (bar)]
(do-thing foo)))

(macroexpand-1
'(js-await [foo (bar)]
(catch e :yo))))

0 comments on commit 05f2cd1

Please sign in to comment.