diff --git a/docs/generator.md b/docs/generator.md index 60f87bd..bb4e0c6 100644 --- a/docs/generator.md +++ b/docs/generator.md @@ -62,6 +62,24 @@ Generator函数的本质,其实是提供一种可以暂停执行的函数。yi yield语句与return语句有点像,都能返回紧跟在语句后面的那个表达式的值。区别在于每次遇到yield,函数暂停执行,下一次再从该位置继续向后执行,而return语句不具备位置记忆的功能。 +Generator函数可以不用yield语句,这时就变成了一个单纯的暂缓执行函数。 + +```javascript + +function* f() { + console.log('执行了!') +} + +var generator = f(); + +setTimeout(function () { + generator.next() +}, 2000); + +``` + +上面代码中,只有调用next方法时,函数f才会执行。 + ## next方法的参数 yield语句本身没有返回值,或者说总是返回undefined。next方法可以带一个参数,该参数就会被当作上一个yield语句的返回值。 diff --git a/docs/promise.md b/docs/promise.md index 1c8193d..0dc1cc7 100644 --- a/docs/promise.md +++ b/docs/promise.md @@ -61,8 +61,11 @@ var getJSON = function(url) { function handler() { if (this.readyState === this.DONE) { - if (this.status === 200) { resolve(this.response); } - else { reject(this); } + if (this.status === 200) { + resolve(this.response); + } else { + reject(this); + } } }; }); @@ -118,6 +121,7 @@ getJSON("/posts.json").then(function(posts) { // some code }).catch(function(error) { // 处理前一个回调函数运行时发生的错误 + console.log('发生错误!', error); }); ``` diff --git a/docs/reference.md b/docs/reference.md index 21bf149..ed5dac8 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -31,11 +31,13 @@ - Matt Baker, [Replacing callbacks with ES6 Generators](http://flippinawesome.org/2014/02/10/replacing-callbacks-with-es6-generators/) - Steven Sanderson, [Experiments with Koa and JavaScript Generators](http://blog.stevensanderson.com/2013/12/21/experiments-with-koa-and-javascript-generators/) - jmar777, [What's the Big Deal with Generators?](http://devsmash.com/blog/whats-the-big-deal-with-generators) +- Marc Harter, [Generators in Node.js: Common Misconceptions and Three Good Use Cases](http://strongloop.com/strongblog/how-to-generators-node-js-yield-use-cases/): 讨论Generator函数的作用 ## Promise对象 - Jake Archibald, [JavaScript Promises: There and back again](http://www.html5rocks.com/en/tutorials/es6/promises/) - Tilde, [rsvp.js](https://github.com/tildeio/rsvp.js) +- Sandeep Panda, [An Overview of JavaScript Promises](http://www.sitepoint.com/overview-javascript-promises/): ES6 Promise入门介绍 ## 工具