A potentially interesting case to test:
async function quux() {
throw "quux failed"
return "unlucky"
}
async function go() {
try {
let x = await quux()
console.log("finished!", x)
} catch {
console.log("caught around await!")
}
}
go()
This pattern of using try/catch around await is common, I think, and the catch code runs even though the error was thrown inside the promise. On my computer in node this program prints "caught around await!"
cljs users may have a similar expectation about how try, await, catch would interact.I think the test suite already tests this
I saw this test: https://github.com/clojure/clojurescript/pull/303/files#diff-0ace430754068ef427130bf7b8b9be4e5166f7d1eee8bdc90d9ec28a268732edR126-R136 and I may be misreading it, but await-in-throw isn't the same as throw-in-await. Perhaps there's another test I overlooked?
I'm pretty sure that variation is going to work, but I can add it later. I guess I can replace (prn should not reach here) with (is false) as a better "should not reach here" as well.
I'll combine it with any other comments @dnolen might have
@hhausman this test would do it right?
(deftest throw-in-await-test
(async done
(let [f (^:async fn [x] (throw "dude") x)]
(try
(let [_ (await (f 2))]
(is false))
(catch :default e
(is (= "dude" e)))
(finally (done))))))We already had this test more or less in this one:
(deftest await-in-throw-test
(async done
(let [f (^:async fn [x] (inc (if (odd? x) (throw (await (js/Promise.resolve "dude"))) x)))]
(try
(let [x (await (f 2))]
(is (= 3 x)))
(let [x (try (await (f 1))
(catch :default e e))]
(is (= "dude" x)))
(catch :default _ (is false))
(finally (done))))))
We test both awaiting in throw, but also throwing in awaitlooks good to me 🙂