@@ -14,7 +14,7 @@ marvelous. They allow you to write promise-based code as if it were synchronous,
14
14
but without blocking the main thread. They make your asynchronous code less
15
15
"clever" and more readable.
16
16
17
- Async functions look like this:
17
+ Async functions work like this:
18
18
19
19
async function myFirstAsyncFunction() {
20
20
try {
@@ -33,32 +33,6 @@ the promise rejects, the rejected value is thrown.
33
33
Note: If you're unfamiliar with promises & promise terminology, check out [ our
34
34
promises guide] ( /web/fundamentals/getting-started/primers/promises ) .
35
35
36
- Calling an async function returns a promise for whatever the function returns or
37
- throws. So with:
38
-
39
- // wait ms milliseconds
40
- function wait(ms) {
41
- return new Promise(r => setTimeout(r, ms));
42
- }
43
-
44
- async function hello() {
45
- await wait(500);
46
- return 'world';
47
- }
48
-
49
- …calling ` hello() ` returns a promise that * fulfills* with ` "world" ` .
50
-
51
- async function foo() {
52
- await wait(500);
53
- throw Error('bar');
54
- }
55
-
56
- …calling ` foo() ` returns a promise that * rejects* with ` Error('bar') ` .
57
-
58
- Note: You can only use ` await ` directly inside an async function. There's some
59
- discussion to allow top-level ` await ` in modules, but it's [ somewhat
60
- controversial] ( https://gist.github.com/Rich-Harris/0b6f317657f5167663b493c722647221 ) .
61
-
62
36
## Example: Logging a fetch
63
37
64
38
Say we wanted to fetch a URL and log the response as text. Here's how it looks
@@ -89,6 +63,30 @@ And here's the same thing using async functions:
89
63
It's the same number of lines, but all the callbacks are gone. This makes it way
90
64
easier to read, especially for those less familiar with promises.
91
65
66
+ ## Return values
67
+
68
+ Calling an async function returns a promise for whatever the function returns or
69
+ throws. So with:
70
+
71
+ // wait ms milliseconds
72
+ function wait(ms) {
73
+ return new Promise(r => setTimeout(r, ms));
74
+ }
75
+
76
+ async function hello() {
77
+ await wait(500);
78
+ return 'world';
79
+ }
80
+
81
+ …calling ` hello() ` returns a promise that * fulfills* with ` "world" ` .
82
+
83
+ async function foo() {
84
+ await wait(500);
85
+ throw Error('bar');
86
+ }
87
+
88
+ …calling ` foo() ` returns a promise that * rejects* with ` Error('bar') ` .
89
+
92
90
## Other async function syntax
93
91
94
92
We've seen ` async function() {} ` already, but the ` async ` keyword can be used
@@ -133,7 +131,7 @@ function to complete before calling the second.
133
131
const storage = new Storage();
134
132
storage.getAvatar('jaffathecake').then(…);
135
133
136
- Note: Class constructors cannot be async.
134
+ Note: Class constructors and getters/settings cannot be async.
137
135
138
136
## Example: Streaming a response
139
137
@@ -187,8 +185,8 @@ Let's try that again with async functions:
187
185
All the "smart" is gone. The asynchronous loop that made me feel so smug is
188
186
replaced with a trusty, boring, while-loop. Much better. In future, we'll get
189
187
[ async iterators] ( https://github.com/tc39/proposal-async-iteration ) {:
190
- .external}, which would [ replace the above ` while `
191
- loop] ( https://gist.github.com/jakearchibald/0b37865637daf884943cf88c2cba1376 ) {:
188
+ .external}, which would [ replace the ` while `
189
+ loop with a for-of loop ] ( https://gist.github.com/jakearchibald/0b37865637daf884943cf88c2cba1376 ) {:
192
190
.external}, making it even neater.
193
191
194
192
Note: I'm sort-of in love with streams. If you're unfamiliar with streaming,
0 commit comments