shadow-cljs icon indicating copy to clipboard operation
shadow-cljs copied to clipboard

Issue with generated code using core async?

Open zehcnas34 opened this issue 6 years ago • 6 comments

Hi, I'm trying to make an async function equivalent in cljs and here's what I have.

(defmacro defn*
  [name args & body]
  `(defn ~name ~args
     (js/Promise.
       (fn [res# rej#]
         (cljs.core.async/go
           (let [v# (try ~@body (catch js/Error e# e#))]
             (if (instance? js/Error v#)
               (rej# v#)
               (res# v#))))))))

I'm using cljs.core.async to emulate the await keyword, I'm running into an issue where the catch branch is not being executed when explicitly throwing an error.

Here's a some sample code using the macro.

(defn* add [a b]
  (<! (timeout 1000))
  (throw "I can't add..."))

with the console error message

Uncaught I can't add...

zehcnas34 avatar Oct 06 '20 06:10 zehcnas34

This might be way off the mark, but are you sure the Uncaught message is from 'inside' your promise or is it being thrown because your promise is being rejected and there is nothing catching that rejection?

theophilusx avatar Oct 06 '20 07:10 theophilusx

As @theophilusx suggested it looks like you don't catch the promise rejection?

(-> (add 1 2)
    (.catch (fn [err] ...))

thheller avatar Oct 06 '20 07:10 thheller

also, you might find the following of interest (taking from clojurescript.org site guide on using promises and async/await. Using Promises with core.async

ClojureScript offers excellent facilities for async programming in core.async. One especially handy tool is the <p! macro, that consumes a promise inside a go block.

Using go blocks allows us to write code that looks synchronous even though it’s actually asynchronous, exactly like await and async do in JavaScript.

ClojureScript:

(:require [cljs.core.async :refer [go]] [cljs.core.async.interop :refer-macros [<p!]])

(def puppeteer (js/require "puppeteer"))

(go (let [browser (<p! (.launch puppeteer)) page (<p! (.newPage browser))] (try (<p! (.goto page "https://clojure.org")) (<p! (.screenshot page #js{:path "screenshot.png"})) (catch js/Error err (js/console.log (ex-cause err)))) (.close browser)))

This is just scratching the surface. core.async gives you very powerful queue-like channels that can do much more than handle one-off promises.

You can read more about core-async in the repository, rationale, code walkthrough, and blog post.

theophilusx avatar Oct 06 '20 08:10 theophilusx

This might be way off the mark, but are you sure the Uncaught message is from 'inside' your promise or is it being thrown because your promise is being rejected and there is nothing catching that rejection?

As @theophilusx suggested it looks like you don't catch the promise rejection?

(-> (add 1 2)
    (.catch (fn [err] ...))

Yup, I wrapped the promise via

(.catch (add 1 2) (fn [e] (js/console.log "ignore error")))

and still received an uncaught exception.

zehcnas34 avatar Oct 06 '20 12:10 zehcnas34

also, you might find the following of interest (taking from clojurescript.org site guide on using promises and async/await. Using Promises with core.async ClojureScript offers excellent facilities for async programming in core.async. One especially handy tool is the <p! macro, that consumes a promise inside a go block. Using go blocks allows us to write code that looks synchronous even though it’s actually asynchronous, exactly like await and async do in JavaScript. ClojureScript: (:require [cljs.core.async :refer [go]] [cljs.core.async.interop :refer-macros [<p!]]) (def puppeteer (js/require "puppeteer")) (go (let [browser (<p! (.launch puppeteer)) page (<p! (.newPage browser))] (try (<p! (.goto page "https://clojure.org")) (<p! (.screenshot page #js{:path "screenshot.png"})) (catch js/Error err (js/console.log (ex-cause err)))) (.close browser))) This is just scratching the surface. core.async gives you very powerful queue-like channels that can do much more than handle one-off promises. You can read more about core-async in the repository, rationale, code walkthrough, and blog post.

Yeah, I've read through it a bit. I'm just trying to make working with some browser API's a little bit nicer.

IMO, I think this sample function is less noisy

(defn* create-conn [id]
  (let [conn     (<p! (js/RTCPeerConnection. {}))
        key-pair (<p! (.. js/window -subtle (generateKey {})))]
    (listen conn "data" handle-channel)
    (on signal "ice" handle-ice)
    conn))

Than

(defn create-conn [id]
  (js/Promise.
    (fn [res rej]
      (go (let [conn (<p! (js/RTCPeerConnection. {}))
                key-pair (<p! (.. js/window -subtle (generateKey {})))]
            (listen conn "data" handle-channel)
            (on signal "ice" handle-ice)
            (res output))))))

Also, when writing many of these functions, I feel like the boilerplate adds up.

zehcnas34 avatar Oct 06 '20 12:10 zehcnas34

Why are you trying to combine core async and promises? It is usually not a good idea to mix two different "async" implementations too much. BEst to either just stick to Promises or core.async not both.

There are a couple known issues in core.async regarding exception handling. So you may be running into any of those.

None of this is related to shadow-cljs so I'll close this.

thheller avatar Oct 06 '20 13:10 thheller