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

Commit 8941d40

Browse files
committed
Feedback edits
1 parent ac53224 commit 8941d40

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ marvelous. They allow you to write promise-based code as if it were synchronous,
1414
but without blocking the main thread. They make your asynchronous code less
1515
"clever" and more readable.
1616

17-
Async functions look like this:
17+
Async functions work like this:
1818

1919
async function myFirstAsyncFunction() {
2020
try {
@@ -33,32 +33,6 @@ the promise rejects, the rejected value is thrown.
3333
Note: If you're unfamiliar with promises & promise terminology, check out [our
3434
promises guide](/web/fundamentals/getting-started/primers/promises).
3535

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-
6236
## Example: Logging a fetch
6337

6438
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:
8963
It's the same number of lines, but all the callbacks are gone. This makes it way
9064
easier to read, especially for those less familiar with promises.
9165

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+
9290
## Other async function syntax
9391

9492
We've seen `async function() {}` already, but the `async` keyword can be used
@@ -133,7 +131,7 @@ function to complete before calling the second.
133131
const storage = new Storage();
134132
storage.getAvatar('jaffathecake').then(…);
135133

136-
Note: Class constructors cannot be async.
134+
Note: Class constructors and getters/settings cannot be async.
137135

138136
## Example: Streaming a response
139137

@@ -187,8 +185,8 @@ Let's try that again with async functions:
187185
All the "smart" is gone. The asynchronous loop that made me feel so smug is
188186
replaced with a trusty, boring, while-loop. Much better. In future, we'll get
189187
[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){:
192190
.external}, making it even neater.
193191

194192
Note: I'm sort-of in love with streams. If you're unfamiliar with streaming,

0 commit comments

Comments
 (0)