@@ -30,7 +30,7 @@ If you use the `async` keyword before a function definition, you can then use
30
30
until the promise settles. If the promise fulfills, you get the value back. If
31
31
the promise rejects, the rejected value is thrown.
32
32
33
- Note: If you're unfamiliar with promises & promise terminology , check out [ our
33
+ Note: If you're unfamiliar with promises, check out [ our
34
34
promises guide] ( /web/fundamentals/getting-started/primers/promises ) .
35
35
36
36
## Example: Logging a fetch
@@ -63,7 +63,7 @@ And here's the same thing using async functions:
63
63
It's the same number of lines, but all the callbacks are gone. This makes it way
64
64
easier to read, especially for those less familiar with promises.
65
65
66
- ## Return values
66
+ ## Async return values
67
67
68
68
Calling an async function returns a promise for whatever the function returns or
69
69
throws. So with:
@@ -87,56 +87,10 @@ throws. So with:
87
87
88
88
…calling ` foo() ` returns a promise that * rejects* with ` Error('bar') ` .
89
89
90
- ## Other async function syntax
91
-
92
- We've seen ` async function() {} ` already, but the ` async ` keyword can be used
93
- with other function syntax:
94
-
95
- ### Arrow functions
96
-
97
- // map some URLs to json-promises
98
- const jsonPromises = urls.map(async url => {
99
- const response = await fetch(url);
100
- return response.json();
101
- });
102
-
103
- Note: ` array.map(func) ` doesn't care that I gave it an async function, it just
104
- sees it as a function that returns a promise. It won't wait for the first
105
- function to complete before calling the second.
106
-
107
- ### Object methods
108
-
109
- const storage = {
110
- async getAvatar(name) {
111
- const cache = await caches.open('avatars');
112
- return cache.match(`/avatars/${name}.jpg`);
113
- }
114
- };
115
-
116
- storage.getAvatar('jaffathecake').then(…);
117
-
118
- ### Class methods
119
-
120
- class Storage {
121
- constructor() {
122
- this.cachePromise = caches.open('avatars');
123
- }
124
-
125
- async getAvatar(name) {
126
- const cache = await this.cachePromise;
127
- return cache.match(`/avatars/${name}.jpg`);
128
- }
129
- }
130
-
131
- const storage = new Storage();
132
- storage.getAvatar('jaffathecake').then(…);
133
-
134
- Note: Class constructors and getters/settings cannot be async.
135
-
136
90
## Example: Streaming a response
137
91
138
92
The benefit of async functions increases in more complex examples. Say we wanted
139
- to stream a response while logging out the chunks and returning the final size.
93
+ to stream a response while logging out the chunks, and return the final size.
140
94
141
95
Note: The phrase "logging out the chunks" made me sick in my mouth.
142
96
@@ -170,13 +124,15 @@ Let's try that again with async functions:
170
124
async function getResponseSize(url) {
171
125
const response = await fetch(url);
172
126
const reader = response.body.getReader();
173
- let result;
127
+ let result = await reader.read() ;
174
128
let total = 0;
175
129
176
- while ((result = await reader.read()) && !result.done) {
130
+ while (!result.done) {
177
131
const value = result.value;
178
132
total += value.length;
179
133
console.log('Received chunk', value);
134
+ // get the next result
135
+ result = await reader.read();
180
136
}
181
137
182
138
return total;
@@ -194,6 +150,52 @@ Note: I'm sort-of in love with streams. If you're unfamiliar with streaming,
194
150
guide] ( https://jakearchibald.com/2016/streams-ftw/#streams-the-fetch-api ) {:
195
151
.external}.
196
152
153
+ ## Other async function syntax
154
+
155
+ We've seen ` async function() {} ` already, but the ` async ` keyword can be used
156
+ with other function syntax:
157
+
158
+ ### Arrow functions
159
+
160
+ // map some URLs to json-promises
161
+ const jsonPromises = urls.map(async url => {
162
+ const response = await fetch(url);
163
+ return response.json();
164
+ });
165
+
166
+ Note: ` array.map(func) ` doesn't care that I gave it an async function, it just
167
+ sees it as a function that returns a promise. It won't wait for the first
168
+ function to complete before calling the second.
169
+
170
+ ### Object methods
171
+
172
+ const storage = {
173
+ async getAvatar(name) {
174
+ const cache = await caches.open('avatars');
175
+ return cache.match(`/avatars/${name}.jpg`);
176
+ }
177
+ };
178
+
179
+ storage.getAvatar('jaffathecake').then(…);
180
+
181
+ ### Class methods
182
+
183
+ class Storage {
184
+ constructor() {
185
+ this.cachePromise = caches.open('avatars');
186
+ }
187
+
188
+ async getAvatar(name) {
189
+ const cache = await this.cachePromise;
190
+ return cache.match(`/avatars/${name}.jpg`);
191
+ }
192
+ }
193
+
194
+ const storage = new Storage();
195
+ storage.getAvatar('jaffathecake').then(…);
196
+
197
+ Note: Class constructors and getters/settings cannot be async.
198
+
197
199
## Careful! Avoid going too sequential
198
200
199
201
Although you're writing code that looks synchronous, ensure you don't miss the
0 commit comments