Skip to content
Tech News
← Back to articles

ClojureScript Gets Async/Await

read original get ClojureScript Async/Await Guide → more articles
Why This Matters

The addition of async/await support in ClojureScript marks a significant step towards better interoperability with modern JavaScript APIs, simplifying asynchronous programming for developers. This enhancement reduces dependency overhead and streamlines integration with browser features and libraries, making ClojureScript more attractive for contemporary web development.

Key Takeaways

Now that ClojureScript targets ECMAScript 2016 we can carefully choose new areas of enhanced interop. Starting with this release, hinting a function as ^:async will make the ClojureScript compiler emit an JavaScript async function:

(refer-global :only '[Promise]) (defn ^:async foo [n] (let [x (await (Promise/resolve 10)) y (let [y (await (Promise/resolve 20))] (inc y)) ;; not async f (fn [] 20)] (+ n x y (f))))

This also works for tests:

(deftest ^:async defn-test (try (let [v (await (foo 10))] (is (= 61 v))) (let [v (await (apply foo [10]))] (is (= 61 v))) (catch :default _ (is false))))

In the last Clojure survey support for async functions dominated the list of desired ClojureScript enhancements for JavaScript interop. This enhancement eliminates the need to take on additional dependencies for the common cases of interacting with modern Browser APIs and popular libraries.