Skip to content

Commit 6b4f156

Browse files
authored
Update translation of 1-js/11-async/08-async-await (javascript-tutorial#656)
Update translation of 1-js/11-async/08-async-await (javascript-tutorial#656)
1 parent 0517cf8 commit 6b4f156

7 files changed

Lines changed: 74 additions & 105 deletions

File tree

1-js/11-async/08-async-await/01-rewrite-async/solution.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ loadJson('no-such-user.json')
2020
解析:
2121

2222
1. 将函数 `loadJson` 变为 `async`
23-
2. 将所有的 `.then` 替换为 `await`
24-
3. 我们也可以不等待,直接 `return response.json()`,像这样:
23+
2. 将函数中所有的 `.then` 都替换为 `await`
24+
3. 我们可以返回 `return response.json()` 而不用等待它,像这样:
2525

2626
```js
2727
if (response.status == 200) {
2828
return response.json(); // (3)
2929
}
3030
```
3131

32-
然后外部的代码就可以用 `await` 来等待这个 promise 被决议。在本例中可忽略。
33-
4. `loadJson` 抛出的错误被 `.catch` 处理了。并且我们不能用 `await loadJson(…)`,因为不是在 `async` 函数中。
34-
32+
然后外部的代码就必须 `await` 这个 promise resolve。在本例中它无关紧要。
33+
4. `loadJson` 抛出的 error 被 `.catch` 处理了。在这儿我们我们不能使用 `await loadJson(…)`,因为我们不是在一个 `async` 函数中。

1-js/11-async/08-async-await/01-rewrite-async/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# 用 async/await 来重写
33

4-
<info:promise-chaining> 章节一个例子中的 `.then/catch` 重写为 `async/await`
4+
重写下面这个来自 <info:promise-chaining> 一章的示例代码,使用 `async/await` 而不是 `.then/catch`
55

66
```js run
77
function loadJson(url) {
@@ -15,6 +15,6 @@ function loadJson(url) {
1515
})
1616
}
1717

18-
loadJson('no-such-user.json') // (3)
18+
loadJson('no-such-user.json')
1919
.catch(alert); // Error: 404
2020
```

1-js/11-async/08-async-await/02-rewrite-async-2/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
这里没有什么技巧只需要将 `demoGithubUser` 中的 `.catch` 替换为 `try...catch`,然后在需要的地方加上 `async/await` 即可:
2+
这里没有什么技巧只需要将 `demoGithubUser` 中的 `.catch` 替换为 `try...catch`,然后在需要的地方加上 `async/await` 即可:
33

44
```js run
55
class HttpError extends Error {
@@ -19,7 +19,7 @@ async function loadJson(url) {
1919
}
2020
}
2121

22-
// 查询用户名直到 github 返回一个合法的用户
22+
// 询问用户名,直到 github 返回一个合法的用户
2323
async function demoGithubUser() {
2424

2525
let user;
@@ -28,13 +28,13 @@ async function demoGithubUser() {
2828

2929
try {
3030
user = await loadJson(`https://api.github.com/users/${name}`);
31-
break; // 没有错误,退出循环
31+
break; // 没有 error,退出循环
3232
} catch(err) {
3333
if (err instanceof HttpError && err.response.status == 404) {
34-
// 循环将在警告后继续
34+
// 循环将在 alert 后继续
3535
alert("No such user, please reenter.");
3636
} else {
37-
// 未知错误,rethrow
37+
// 未知的 error,再次抛出(rethrow
3838
throw err;
3939
}
4040
}

1-js/11-async/08-async-await/02-rewrite-async-2/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
# async/await 来重写「rethrow
2+
# 使用 async/await 重写 "rethrow"
33

4-
下面你可以看到 <info:promise-chaining> 章节中的「rethrow例子。让我们来用 `async/await` 来替换 `.then/catch`
4+
下面你可以看到来自 <info:promise-chaining> 一章的 "rethrow" 例子。让我们来用 `async/await` 重写它,而不是使用 `.then/catch`
55

6-
同时我们可以在 `demoGithubUser` 中用循环代替递归:`async/await` 让这将变得更加容易
6+
同时,我们可以在 `demoGithubUser` 中使用循环以摆脱递归:在 `async/await` 的帮助下很容易实现
77

88
```js run
99
class HttpError extends Error {
@@ -25,7 +25,7 @@ function loadJson(url) {
2525
})
2626
}
2727

28-
// 查询用户名直到 github 返回一个合法的用户
28+
// 询问用户名,直到 github 返回一个合法的用户
2929
function demoGithubUser() {
3030
let name = prompt("Enter a name?", "iliakan");
3131

1-js/11-async/08-async-await/03-async-from-regular/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
这个例子告诉我们知道内部是如何运行的会很有帮助
2+
在这种情况下,知道其内部工作原理会很有帮助
33

4-
只需要把 `async` 函数返回值当成 promise,并且在后面加上 `.then` 即可:
4+
只需要把 `async` 调用当作 promise 对待,并在它的后面加上 `.then` 即可:
55
```js run
66
async function wait() {
77
await new Promise(resolve => setTimeout(resolve, 1000));
@@ -10,7 +10,7 @@ async function wait() {
1010
}
1111

1212
function f() {
13-
// 一秒后显示 10
13+
// 1 秒后显示 10
1414
*!*
1515
wait().then(result => alert(result));
1616
*/!*
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
# Call async from non-async
2+
# 在非 async 函数中调用 async 函数
33

4-
我们在一个「普通的」函数中,如何调用另一个 `async` 函数并且拿到返回值
4+
我们有一个“普通”函数。如何在这个函数中调用 `async` 函数并使用其结果
55

66
```js
77
async function wait() {
@@ -12,9 +12,9 @@ async function wait() {
1212

1313
function f() {
1414
// ...这里怎么写?
15-
// 我们需要调用 async wait() 等待并拿到结果 10
16-
// 记住, 我们不能使用 await
15+
// 我们需要调用 async wait() 并等待以拿到结果 10
16+
// 记住我们不能使用 "await"
1717
}
1818
```
1919

20-
P.S. 这个任务很简单,但是对于 async/await 新手来说却很常见
20+
P.S. 这个任务其实很简单,但是对于 async/await 新手开发者来说,这个问题却很常见

0 commit comments

Comments
 (0)