# How to deal with js/async and js/await in ClojureScript?

**URL:** https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234
**Category:** How to?
**Tags:** clojurescript
**Created:** [August 11, 2023, 11:17pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234 "2023-08-11T23:17:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [August 11, 2023, 11:17pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/1 "2023-08-11T23:17:26Z")

</div>

I am implementing in Clojurescript (re-frame) code that looks like this:

```javascript
const login = async () => {
  await auth0Client.loginWithRedirect({
    authorizationParams: {
      redirect_uri: window.location.origin
    }
  });
};

```

But I am not sure how best to translate the async and the await commands. Any suggestions?

---

<div class="post-metadata">

### Author: ![Webdev\_Tory](https://clojureverse.org/user_avatar/clojureverse.org/webdev_tory/32/3781_2.png) [@Webdev\_Tory](https://clojureverse.org/u/Webdev_Tory)
#### Post date: [August 11, 2023, 11:23pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/2 "2023-08-11T23:23:14Z")

</div>

I swear this documentation wasn’t here when I looked previously… [ClojureScript - Promise interop](https://clojurescript.org/guides/promise-interop)

---

<div class="post-metadata">

### Author: ![thheller](https://clojureverse.org/user_avatar/clojureverse.org/thheller/32/534_2.png) [@thheller](https://clojureverse.org/u/thheller)
#### Post date: [August 12, 2023, 6:18am UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/3 "2023-08-12T06:18:22Z")

</div>

If you are using shadow-cljs there is also

> [@Promise handling in CLJS using js-await](https://v2.clojureverse.org/t/promise-handling-in-cljs-using-js-await/8998):
>
> Recently I had to write some code using Promises and thought it was time for some syntax sugar. So I added a very simplistic macro to basically emulate what JS await does while actually mapping to traditional Promise .then and .catch. This way the macro can stay extremely simple without having to modify the compiler to add support for async functions and actual await. That is a quest for another day. Currently this is only available as part of shadow-cljs (since 2.19.1) but the [macro](https://github.com/thheller/shadow-cljs/blob/49fb078b834e64f63122e3a8ad3ddcd1f4485969/src/main/shadow/cljs/modern.cljc#L310-L326) you can ea…

But you picked an example that doesn’t need either the `async` keyword nor the `await`.

This is functionally equivalent to the async/await JS variant you posted.

```clojure
(defn login []
  (.loginWithRedirect auth0client
    (clj->js
      {:authorizationParams
       {:redirect_uri js/window.location.origin}})))

```

Or if you really must use async and maybe want to do something with the result, you can do so with `js-await` from shadow-cljs.

```auto
(defn login []
  (js-await [result
             (.loginWithRedirect auth0client
               (clj->js
                 {:authorizationParams
                  {:redirect_uri js/window.location.origin}}))]
    result
    (catch failure
      (prn [:login-failed failure])
      ::fail!)))

;; or regular old .then interop
(defn login []
  (-> (.loginWithRedirect auth0client
        (clj->js
          {:authorizationParams
           {:redirect_uri js/window.location.origin}}))
      (.then (fn [result]
               result))
      (.catch (fn [failure]
                (prn [:login-failed failure])
                failure))))

```

An `async` function in JS is nothing but a regular function that returns a `Promise`. If all you do is call a function that returns a promise then you can just return that and you are done. You can of course to `.then` interop if you want. I strongly recommend NOT using core.async for Promise interop, **if that is your only use of core.async**. It is fine if you are already using core.async elsewhere.

---

<div class="post-metadata">

### Author: ![greinseth](https://clojureverse.org/user_avatar/clojureverse.org/greinseth/32/1815_2.png) [@greinseth](https://clojureverse.org/u/greinseth)
#### Post date: [August 12, 2023, 9:47pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/4 "2023-08-12T21:47:59Z")

</div>

Why do you recommend not using core.async? Larger bundle size?

---

<div class="post-metadata">

### Author: ![thheller](https://clojureverse.org/user_avatar/clojureverse.org/thheller/32/534_2.png) [@thheller](https://clojureverse.org/u/thheller)
#### Post date: [August 13, 2023, 6:25am UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/5 "2023-08-13T06:25:47Z")

</div>

I specifically highlighted “ **if that is your only use of core.async** ”. So, if you only use of core.async is promise handling then it is absolutely wasteful. Build size, performance and general ergonomics just suffer without any benefit. You write more verbose code and lose access to the underlying Promise, which in some contexts (e.g. Debugging, Inspection) can be very useful.

---

<div class="post-metadata">

### Author: ![tobyloxy](https://clojureverse.org/user_avatar/clojureverse.org/tobyloxy/32/2695_2.png) [@tobyloxy](https://clojureverse.org/u/tobyloxy)
#### Post date: [August 14, 2023, 6:17am UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/6 "2023-08-14T06:17:49Z")

</div>

OP, not sure if you’ve ever used promesa library but I find it makes working with promises very convenient, especially the p/let form. Basically the RHS of each binding pair is resolved into a value before passing any further down. Just be aware that the @ syntax in the docs only works in CLJ.

[https://funcool.github.io/promesa/latest/promises.html](https://funcool.github.io/promesa/latest/promises.html)

Example:

```auto
;; Return value of this p/let block is a promise for the last expression
(p/let [result (this-function-returns-a-promise)]
  ;; do-something-with-result expects a value as it's argument, not a promise
  (do-something-with-result result)) ;; Here result is a value, no longer a promise

```

---

<div class="post-metadata">

### Author: ![greinseth](https://clojureverse.org/user_avatar/clojureverse.org/greinseth/32/1815_2.png) [@greinseth](https://clojureverse.org/u/greinseth)
#### Post date: [August 14, 2023, 1:05pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/7 "2023-08-14T13:05:27Z")

</div>

Ok, good to know. I have little experience with core.async, is the performance that bad? I don’t find the ergonomics in the linked example bad, though. Granted, it may be an unnecessary abstraction over Promises when used solely for that, but might be worth it to get to know core.async? I don’t know…

---

<div class="post-metadata">

### Author: ![thheller](https://clojureverse.org/user_avatar/clojureverse.org/thheller/32/534_2.png) [@thheller](https://clojureverse.org/u/thheller)
#### Post date: [August 14, 2023, 3:47pm UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/8 "2023-08-14T15:47:55Z")

</div>

Performance is fine, but on the client your resource budget is already limited as it is. If you can save time and bandwidth, then you should. `core.async` for just promise handling is just overkill and not what it was designed for (i.e. CSP). You of course can but to me that always feels like getting in my car and drive 10 meters instead of just walking. 😛

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [February 13, 2024, 3:48am UTC](https://clojureverse.org/t/how-to-deal-with-js-async-and-js-await-in-clojurescript/10234/9 "2024-02-13T03:48:39Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
