![Nick Alexander](/assets/img/avatar_default.png)
This is a well-worn idea: use a `promise-channel` of `[result nil]` or `[nil error]` pairs. The `go-pair` and `<?` macros handle catching exceptions (important, given that synchronous CLJ code expects to throw rather than return an error promise or similar), allowing code like: ``` (go-pair (let [result (<? (pair-chan-fn))] (when (not result) (throw (Exception. "No result!"))) (transform result))) ``` to be expressed naturally. These are the equivalents of `async` and `await` in JS. The implementation is complicated by significant incompatibilities between CLJ and CLJS. The solution presented here takes care to separate the macro definitions into CLJ. Sadly, this requires namespacing the per-environment symbols explicitly; but we hope to minimize such code in files like this. The most significant restriction to this approach is that consumers must require the transitive dependencies of the macro-defining modules. See the included tests (both CLJ and CLJS) for the appropriate incantations (for pair-chan, core.async, and test).
17 lines
551 B
Clojure
17 lines
551 B
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
(ns datomish.test-macros-test
|
|
(:require-macros
|
|
[datomish.pair-chan :refer [go-pair]]
|
|
[datomish.test-macros :refer [deftest-async]]
|
|
[cljs.core.async.macros])
|
|
(:require [cljs.core.async]
|
|
[cljs.test :refer-macros [is are deftest testing async]]))
|
|
|
|
(deftest sync-test
|
|
(is (= 1 1)))
|
|
|
|
(deftest-async async-test
|
|
(is (= 1 1)))
|