Skip to content

Commit c22ff50

Browse files
authored
Enhancements to async/await reference (#424)
* Enhancements to async/await reference * add word * style changes
1 parent 771cbc6 commit c22ff50

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

content/reference/async-functions.adoc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ until the `Promise` resolves and yields its value.
2020

2121
[source,clojure]
2222
----
23-
(^:async fn [n]
24-
(let [x (await (js/Promise.resolve 10))]
25-
(+ n x)))
26-
----
23+
(refer-global :only '[Promise console fetch]) ;; used in examples throughout this reference
2724
28-
[source,clojure]
29-
----
30-
(fn ^:async [n]
31-
(let [x (await (js/Promise.resolve 10))]
25+
(^:async fn [n]
26+
(let [x (await (Promise/resolve 10))]
3227
(+ n x)))
3328
----
3429

3530
[source,clojure]
3631
----
3732
(defn ^:async foo [n]
38-
(let [x (await (js/Promise.resolve 10))]
33+
(let [x (await (Promise/resolve 10))]
3934
(+ n x)))
4035
----
4136

@@ -50,7 +45,7 @@ These placements are invalid:
5045

5146
[source,clojure]
5247
----
53-
^:async (fn foo [urls] ,,,)
48+
^:async (fn [urls] ,,,)
5449
----
5550

5651
[[error-handling]]
@@ -62,22 +57,28 @@ code.
6257

6358
[source,clojure]
6459
----
65-
(defn ^:async fetch [url]
60+
(defn ^:async fetch-json [url]
6661
(try
67-
(let [resp (await (js/fetch url))]
62+
(let [resp (await (fetch url))]
6863
(await (.json resp)))
6964
(catch :default e
70-
(js/console.log "fetch failed" e)
65+
(.log console "fetch failed" e)
7166
nil)))
7267
----
7368

69+
Note that even though the `await` call is in return position, it is necessary here to
70+
handle the rejection within the scope of the `try/catch`. Without it, the
71+
rejection would have to be handled by the caller of `fetch-json`.
72+
7473
[[testing]]
7574
== Testing
7675

7776
The `cljs.test` `deftest` macro supports `:async` on the name of the test.
7877

7978
[source,clojure]
8079
----
80+
(require '[cljs.test :refer [deftest is]])
81+
8182
(deftest ^:async my-test
8283
(let [v (await (foo 10))]
8384
(is (= 20 v))))
@@ -90,13 +91,14 @@ The `await` macro may only be called inside an `:async` function and the return
9091

9192
CLJS does not support top-level await.
9293

94+
Multi-arity functions cannot be mixed sync and async. This is why CLJS doesn't let you put `:async` on the arg vector.
95+
96+
The `await` macro in CLJS has nothing to do with the `await` function in JVM Clojure. CLJS does not have agents.
97+
9398
Functions created inside an `:async` function are non-async unless also annotated with `:async` metadata. So the following won't work:
9499

95100
[source,clojure]
96101
----
97-
(refer-global :only '[Promise fetch])
98-
99-
;; won't work: inner fn is not :async, so `await` is invalid there
100102
(defn ^:async fetch-statuses [urls]
101103
(map (fn [url] (.-status (await (fetch url)))) urls))
102104
----
@@ -115,11 +117,11 @@ directly. The return value of this function will be a `Promise` resolving to a s
115117
(map (^:async fn [url] (.-status (await (fetch url)))) urls))
116118
----
117119

118-
A possible solution is to use `Promise.all` to await all fetches in parallel:
120+
A possible solution is to use `Promise/all` to await all fetches in parallel:
119121

120122
[source,clojure]
121123
----
122124
(defn ^:async fetch-statuses [urls]
123-
(let [resps (await (Promise.all (mapv fetch urls)))]
125+
(let [resps (await (Promise/all (mapv fetch urls)))]
124126
(map #(.-status %) resps)))
125127
----

0 commit comments

Comments
 (0)