Last active
January 26, 2022 22:18
-
-
Save beders/06eeb1d8f49de715c6bd2b84f634cff6 to your computer and use it in GitHub Desktop.
Super minimal macro to simplify dealing with promise/async/await code in ClojureScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn create-function-call [param expr] | |
"Create an sexp for calling expr with a first argument provided by a promise. | |
If expr is a list (already in form suitable for a function call), insert the first argument at second position, | |
otherwise turn expr into a function call expression, unless the function is an fn, which is simply returned. | |
println -> (fn [param] (println param)) | |
(* 2) -> (fn [param] (* param 2)) | |
(fn [result]) -> (fn [result]) | |
" | |
(if (and (list? expr) (= 'fn (first expr))) ;; expr can be used per se | |
expr | |
(list 'fn [param] | |
(if (list? expr) | |
(conj (conj (rest expr) param) (first expr)) | |
(list expr param) | |
) | |
)) | |
) | |
(defmacro promise-> [promise & body] | |
"Chain promises with an optional :catch clause. Works with any promise implementation. | |
Start with a promise object and then chain as usual. | |
Returns a promise object. | |
(promise-> (js/Promise.resolve 1) inc inc js/console.log) | |
=> #object[Promise [object Promise]] | |
(prints 3 on console) | |
Optionally add one ore more :catch error-handler sexp to register a (.catch ...) function: | |
(promise-> (js/Promise.reject \"error\") inc inc :catch js/console.error) | |
=> #object[Promise [object Promise]] | |
(prints \"error\" on console) | |
" | |
(let [[body-then [_ & body-catch]] (split-with #(not= :catch %) body) | |
param (gensym 'result) | |
] | |
`(-> ~promise | |
~@(map (fn [expr] (list '.then (create-function-call param expr))) body-then) | |
~@(map (fn [expr] (list `.catch (create-function-call param expr))) body-catch) | |
))) | |
With the latest change, you can also use a fn
for the promise callback:
(promise-> (js/Promise.resolve 1) (fn [result] (inc result)))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
promise->
Small chaining macro to deal with promises in ClojureScript
Useful when you are working with JS code that either makes you use callbacks or provides promises (in the form of await or regular Promise objects).
Examples
Note that:
Promise.resolve(aValue);
creates an already resolved promise.As a regular
->
call:A bit gnarly that is.
With the
promise->
macroThis should print 3 on the console.
To deal with rejected promises, add a
:catch
keyword followed by one more or catch handlers.This should print "error" on the console as we start off with a rejected promise.
Here's a real-world example using
fetch
to load JSON and then extract some property.This should print your IP address to the console!
How do I get the result for realz (as a return value!)
You can stick your desired end-result in a
core.async
queue and get it out that way.Or check out Promesa which has many more capabilities.
But, why?!?
Yes, there are various other libraries out there dealing with promises. I've looked at a few of them.
This was a little exercise in writing my first macro. Hope you like it!