6-async/05-async-await#203
Merged
leviding merged 13 commits intojavascript-tutorial:zh-hansfrom Sep 15, 2018
Starriers:translation/async-awa
Merged
6-async/05-async-await#203leviding merged 13 commits intojavascript-tutorial:zh-hansfrom Starriers:translation/async-awa
leviding merged 13 commits intojavascript-tutorial:zh-hansfrom
Starriers:translation/async-awa
Conversation
Contributor
Author
|
promise 涉及 的 settle reject resolve 翻译有问题 |
|
@leviding校对认领 |
Member
|
@YvongYang ok |
YvongYang
reviewed
Sep 2, 2018
| @@ -1,5 +1,5 @@ | |||
|
|
|||
| There are no tricks here. Just replace `.catch` with `try...catch` inside `demoGithubUser` and add `async/await` where needed: | |||
| 没有窍门,只需要在 `demoGithubUser` 中使用 `try...catch` 替换 `.catch` 然后在我们需要的地方添加 `async/await`: | |||
There was a problem hiding this comment.
1.『没有窍门』=>『这里没有窍门』
2.『只需要在 demoGithubUser 中』=>『只是在 demoGithubUser 中』
3.『然后在我们需要的地方』=>『并在所需要的地方』
| ## Async 函数 | ||
|
|
||
| Let's start with the `async` keyword. It can be placed before function, like this: | ||
| 我们从`async` 关键字开始。它可以放在函数前,就像这样: |
| ``` | ||
|
|
||
| ...We could explicitly return a promise, that would be the same: | ||
| ...我们可以明确返回 promise,内容相似: |
| */!* | ||
|
|
||
| alert(result); // "done!" | ||
| alert(result); // “完成!” |
|
|
||
| ````smart header="`await` won't work in the top-level code" | ||
| People who are just starting to use `await` tend to forget that, but we can't write `await` in the top-level code. That wouldn't work: | ||
| ````smart header="`await` 在高级代码中无效" |
| ## 总结 | ||
|
|
||
| The `async` keyword before a function has two effects: | ||
| 函数前的 `async` 关键字有两个副作用: |
|
|
||
| 1. If it's an error, the exception is generated, same as if `throw error` were called at that very place. | ||
| 2. Otherwise, it returns the result, so we can assign it to a value. | ||
| 1. 如果有 error,就会产生异常,就像在那个地方调用了 `throw error` 一样。 |
| 1. If it's an error, the exception is generated, same as if `throw error` were called at that very place. | ||
| 2. Otherwise, it returns the result, so we can assign it to a value. | ||
| 1. 如果有 error,就会产生异常,就像在那个地方调用了 `throw error` 一样。 | ||
| 2. 否则,就会返回值,我们就可以对其赋值。 |
| 2. 否则,就会返回值,我们就可以对其赋值。 | ||
|
|
||
| Together they provide a great framework to write asynchronous code that is easy both to read and write. | ||
| 它们一起为编写易于阅读的异步代码提供了一个很好的框架。 |
| 它们一起为编写易于阅读的异步代码提供了一个很好的框架。 | ||
|
|
||
| With `async/await` we rarely need to write `promise.then/catch`, but we still shouldn't forget that they are based on promises, because sometimes (e.g. in the outermost scope) we have to use these methods. Also `Promise.all` is a nice thing to wait for many tasks simultaneously. | ||
| 对于 `async/await`,我们很少需要编写 `promise.then/catch`,按我们不应该忘记它们是基于 promise 的。因为有时(例如,在最外面的范围)我们必须使用这些方法。同样,对于同时等待许多人物,`Promise.all` 是一中好方法。 |
There was a problem hiding this comment.
- 按我们不应该忘记它们是基于 promise 的。=> 但我们不应该忘记它们是基于 promise 的,
- 因为有时(例如,在最外面的范围)我们必须使用这些方法 => 因为有时(例如,在最外面的范围)我们不得不使用这些方法
- 同样,对于同时等待许多人物,
Promise.all是一中好方法 =>Promise.all也是一个很好的东西,它能够同时等待很多任务。
|
@Starriers 和 @leviding 校对完成 |
Member
Member
|
@Starriers 可以修改了 |
1 similar comment
Member
|
@Starriers 可以修改了 |
leviding
approved these changes
Sep 15, 2018
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.