Skip to content

Instantly share code, notes, and snippets.

@beders
Last active January 26, 2022 22:18
Show Gist options
  • Save beders/06eeb1d8f49de715c6bd2b84f634cff6 to your computer and use it in GitHub Desktop.
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
(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)
)))
@beders
Copy link
Author

beders commented Sep 6, 2018

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:

(-> (js/Promise.resolve 1) 
   (.then (fn [result] (inc result))) 
   (.then (fn [result] (inc result))) 
   (.then (fn [result] (js/console.log result))))
=> #object[Promise [object Promise]]

A bit gnarly that is.

With the promise-> macro

(promise-> (js/Promise.resolve 1) inc inc js/console.log)
=> #object[Promise [object Promise]]

This should print 3 on the console.

To deal with rejected promises, add a :catch keyword followed by one more or catch handlers.

(promise-> (js/Promise.reject "error") inc inc js/console.log :catch js/console.error)
=> #object[Promise [object Promise]]

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.

(promise-> (js/fetch "http://ip.jsontest.com/") .json .-ip js/console.log)
=> #object[Promise [object Promise]]

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!

@beders
Copy link
Author

beders commented Sep 13, 2018

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