ClojureScript Gets Async/Await

原始链接: https://clojurescript.org/news/2026-05-07-release

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. For a complete list of fixes, changes, and enhancements to ClojureScript see here

Hacker Newsnew | past | comments | ask | show | jobs | submitloginClojureScript Gets Async/Await (clojurescript.org)18 points by Borkdude 1 hour ago | hide | past | favorite | 2 comments help zerr 22 minutes ago | next [–] For the moment thought the article was about CoffeeScript... But it already supports async/await :)replymidnight_eclair 26 minutes ago | prev [–] to be clear - clojurescript had support for asynchronous paradigm through core.async library (CSP style) long before async/await landed in javascript itself.reply Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact Search:
相关文章

原文

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.

For a complete list of fixes, changes, and enhancements to ClojureScript see here

联系我们 contact @ memedata.com