Transforms async function bodies with await into Promise .then chains.
Supports await in:
Example: (defn ^:async foo [] (let [x (await (js/Promise.resolve 1))] (inc x)))
Transforms to: (defn foo [] (.then (js/Promise.resolve 1) (fn [x] (inc x))))
Transforms async function bodies with await into Promise .then chains.
Supports await in:
- let bindings: (let [x (await p)] ...)
- do forms: (do (await p) ...)
- arbitrary expressions: (inc (await p))
- macros like -> are expanded first
Example:
(defn ^:async foo []
(let [x (await (js/Promise.resolve 1))]
(inc x)))
Transforms to:
(defn foo []
(.then (js/Promise.resolve 1) (fn [x] (inc x))))(mark-promise form)Mark a form as promise-producing using metadata.
Mark a form as promise-producing using metadata.
(promise-catch promise-expr callback)Create a .catch chain
Create a .catch chain
(promise-catch-for-try promise-expr callback)Create a .catch chain that unwraps SCI error wrapping before calling handler
Create a .catch chain that unwraps SCI error wrapping before calling handler
(promise-finally promise-expr callback)Create a .finally chain
Create a .finally chain
(promise-then promise-expr callback)Create a .then chain
Create a .then chain
(transform-async-body ctx locals body)Walk body and transform await calls. Expands macros first if needed. locals is a set of locally bound symbols that should not be macro-expanded.
Walk body and transform await calls. Expands macros first if needed. locals is a set of locally bound symbols that should not be macro-expanded.
(transform-async-fn-body ctx locals body-exprs)Transform async function body expressions and ensure result is a promise. This is the main entry point for async function transformation.
Transform async function body expressions and ensure result is a promise. This is the main entry point for async function transformation.
(transform-do ctx locals exprs)Transform do with await calls into .then chains.
Transform do with await calls into .then chains.
(transform-expr-with-await ctx locals expr)Transform a general expression, chaining any promise-producing subforms.
Transform a general expression, chaining any promise-producing subforms.
(transform-let* ctx locals bindings body)Transform let* with await calls into .then chains.
Transform let* with await calls into .then chains.
(transform-loop* ctx locals bindings body)Transform loop* with await into a recursive promise-returning function. Wraps init values in let* to preserve sequential scoping (each init sees previous bindings). Returns original loop unchanged if there are no promises.
(loop* [x 0] (if (< x 3) (do (await p) (recur (inc x))) x)) => (let* [x 0] ((fn loop-fn [x] (if (< x 3) (.then p (fn [_] (loop-fn (inc x)))) (js/Promise.resolve x))) x))
Transform loop* with await into a recursive promise-returning function. Wraps init values in let* to preserve sequential scoping (each init sees previous bindings). Returns original loop unchanged if there are no promises. (loop* [x 0] (if (< x 3) (do (await p) (recur (inc x))) x)) => (let* [x 0] ((fn loop-fn [x] (if (< x 3) (.then p (fn [_] (loop-fn (inc x)))) (js/Promise.resolve x))) x))
(transform-try ctx locals exprs)Transform try/catch/finally with await into Promise .catch/.finally chains. Only uses promise chains if there's actually an await somewhere.
Transform try/catch/finally with await into Promise .catch/.finally chains. Only uses promise chains if there's actually an await somewhere.
(wrap-promise expr)Wrap value in promise resolve to handle non-Promise values
Wrap value in promise resolve to handle non-Promise values
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |