Skip to content

Commit 355f0af

Browse files
committed
add the generator and async await translating.
1 parent 5d2d8c5 commit 355f0af

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

README_zhCn.md

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -855,13 +855,10 @@ Promise.all(urlPromises)
855855

856856
## Generators
857857

858-
Similar to how [Promises](https://github.com/DrkSephy/es6-cheatsheet#promises) allow us to avoid
859-
[callback hell](http://callbackhell.com/), Generators allow us to flatten our code - giving our
860-
asynchronous code a synchronous feel. Generators are essentially functions which we can
861-
[pause their excution](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield)
862-
and subsequently return the value of an expression.
858+
就像[Promises](https://github.com/DrkSephy/es6-cheatsheet#promises)如何让我们避免[回调地狱](http://callbackhell.com/)一样,Generators也可以使我们的代码扁平化,同时给予我们开发者像开发同步代码一样的感觉来写异步代码。Generators本质上是一种支持的函数,随后返回表达式的值。
859+
Generators实际上是支持[暂停运行](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield),随后根据上一步的返回值再继续运行的一种函数。
863860

864-
A simple example of using generators is shown below:
861+
下面代码是一个使用generators函数的简单例子:
865862

866863
```javascript
867864
function* sillyGenerator() {
@@ -879,9 +876,8 @@ var value = generator.next();
879876
> console.log(value); // { value: 4, done: false }
880877
```
881878

882-
Where [next](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next)
883-
will allow us to push our generator forward and evaluate a new expression. While the above example is extremely
884-
contrived, we can utilize Generators to write asynchronous code in a synchronous manner:
879+
就像上面的例子,当[next](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/next)运行时,它会把我们的generator向前“推动”,同时执行新的表达式。
880+
我们能利用Generators来像书写同步代码一样书写异步代码。
885881

886882
```javascript
887883
// Hiding asynchronousity with Generators
@@ -893,7 +889,7 @@ function request(url) {
893889
}
894890
```
895891

896-
And here we write a generator function that will return our data:
892+
这里我们写个generator函数将要返回我们的数据:
897893

898894
```javascript
899895
function* getData() {
@@ -904,11 +900,9 @@ function* getData() {
904900
}
905901
```
906902

907-
By the power of `yield`, we are gauranteed that `entry1` will have the data needed to be parsed and stored
908-
in `data1`.
903+
借助于 `yield`,我们可以保证 `entry1` 确实拿到数据并转换后再赋值给 `data1`
909904

910-
While generators allow us to write asynchronous code in a synchronous manner, there is no clear
911-
and easy path for error propagation. As such, as we can augment our generator with Promises:
905+
当我们使用generators来像书写同步代码一样书写我们的异步代码逻辑时,没有一种清晰简单的方式来处理期间可能会产生的错误或者异常。在这种情况下,我们可以在我们的generator中引入Promises来处理,就像下面这样:
912906

913907
```javascript
914908
function request(url) {
@@ -918,8 +912,7 @@ function request(url) {
918912
}
919913
```
920914

921-
And we write a function which will step through our generator using `next` which in turn will utilize our
922-
`request` method above to yield a Promise:
915+
我们再写一个函数,其中使用 `next` 来步进我们的generator的同事,再利用我们上面的 `request` 方法来产生(yield)一个Promise。
923916

924917
```javascript
925918
function iterateGenerator(gen) {
@@ -934,10 +927,8 @@ function iterateGenerator(gen) {
934927
}
935928
```
936929

937-
<sup>[(回到目录)](#table-of-contents)</sup>
938-
939-
By augmenting our Generator with Promises, we have a clear way of propogating errors through the use of our
940-
Promise `.catch` and `reject`. To use our newly augmented Generator, it is as simple as before:
930+
在Generator中引入了Promises后,我们就可以通过Promise的 `.catch``reject` 来捕捉和处理错误了。
931+
使用了我们新版的Generator后,新版的调用就像老版本一样简单可读(译者注:有微调):
941932

942933
```javascript
943934
iterateGenerator(function* getData() {
@@ -948,17 +939,15 @@ iterateGenerator(function* getData() {
948939
});
949940
```
950941

951-
We were able to reuse our implementation to use our Generator as before, which shows their power. While Generators
952-
and Promises allow us to write asynchronous code in a synchronous manner while retaining the ability to propogate
953-
errors in a nice way, we can actually begin to utilize a simpler construction that provides the same benefits:
954-
[async-await](https://github.com/DrkSephy/es6-cheatsheet#async-await).
942+
在使用Generator后,我们可以重用我们的老版本代码实现,以此展示了Generator的力量。
943+
当使用Generators和Promises后,我们可以像书写同步代码一样书写异步代码的同时优雅地解决了错误处理问题。
944+
此后,我们实际上可以开始利用更简单的一种方式了,它就是[async-await](https://github.com/DrkSephy/es6-cheatsheet#async-await)
955945

956946
<sup>[(回到目录)](#table-of-contents)</sup>
957947

958948
## Async Await
959949

960-
While this is actually an upcoming ES2016 feature, `async await` allows us to perform the same thing we accomplished
961-
using Generators and Promises with less effort:
950+
`async await` 随着ES2016版本就要发布了,它给我们提供了一种更轻松的、更简单的可以替代的实现上面 Generators 配合 Promises 组合代码的一种编码方式,让我们来看看例子:
962951

963952
```javascript
964953
var request = require('request');
@@ -979,7 +968,7 @@ async function main() {
979968
main();
980969
```
981970

982-
Under the hood, it performs similarly to Generators. I highly recommend using them over Generators + Promises. A great resource
983-
for getting up and running with ES7 and Babel can be found [here](http://masnun.com/2015/11/11/using-es7-asyncawait-today-with-babel.html).
971+
它们看上去和Generators很像。我(作者)强烈推荐使用 `async await` 来替代Generators + Promises的写法。
972+
[这里](http://masnun.com/2015/11/11/using-es7-asyncawait-today-with-babel.html)是个很好的学习资源,让我们学习和使用这项ES7中的新功能。
984973

985974
<sup>[(回到目录)](#table-of-contents)</sup>

0 commit comments

Comments
 (0)