Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit e89f6f9

Browse files
committed
Reordering based on feedback
1 parent 8941d40 commit e89f6f9

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

src/content/en/fundamentals/getting-started/primers/async-functions.md

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ If you use the `async` keyword before a function definition, you can then use
3030
until the promise settles. If the promise fulfills, you get the value back. If
3131
the promise rejects, the rejected value is thrown.
3232

33-
Note: If you're unfamiliar with promises & promise terminology, check out [our
33+
Note: If you're unfamiliar with promises, check out [our
3434
promises guide](/web/fundamentals/getting-started/primers/promises).
3535

3636
## Example: Logging a fetch
@@ -63,7 +63,7 @@ And here's the same thing using async functions:
6363
It's the same number of lines, but all the callbacks are gone. This makes it way
6464
easier to read, especially for those less familiar with promises.
6565

66-
## Return values
66+
## Async return values
6767

6868
Calling an async function returns a promise for whatever the function returns or
6969
throws. So with:
@@ -87,56 +87,10 @@ throws. So with:
8787

8888
…calling `foo()` returns a promise that *rejects* with `Error('bar')`.
8989

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-
13690
## Example: Streaming a response
13791

13892
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.
14094

14195
Note: The phrase "logging out the chunks" made me sick in my mouth.
14296

@@ -170,13 +124,15 @@ Let's try that again with async functions:
170124
async function getResponseSize(url) {
171125
const response = await fetch(url);
172126
const reader = response.body.getReader();
173-
let result;
127+
let result = await reader.read();
174128
let total = 0;
175129

176-
while ((result = await reader.read()) && !result.done) {
130+
while (!result.done) {
177131
const value = result.value;
178132
total += value.length;
179133
console.log('Received chunk', value);
134+
// get the next result
135+
result = await reader.read();
180136
}
181137

182138
return total;
@@ -194,6 +150,52 @@ Note: I'm sort-of in love with streams. If you're unfamiliar with streaming,
194150
guide](https://jakearchibald.com/2016/streams-ftw/#streams-the-fetch-api){:
195151
.external}.
196152

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+
197199
## Careful! Avoid going too sequential
198200

199201
Although you're writing code that looks synchronous, ensure you don't miss the

0 commit comments

Comments
 (0)