yield next和yield* next的区别
yield next和yield* next之間到底有什么區別?為什么需要yield* next?經常會有人提出這個問題。雖然我們在代碼中會盡量避免使用yield* next以減少新用戶的疑惑,但還是經常會有人問到這個問題。為了體現自由,我們在koa框架內部使用了yield* next,但是為了避免引起混亂我們并不提倡這樣做。
相關文檔,可以查看這里的說明harmony proposal.
yield委托(delegating)做了什么?
假設有下面兩個generator函數:
function* outer() {yield 'open'yield inner()yield 'close' }function* inner() {yield 'hello!' }通過調用函數outer()能產出哪些值呢?
var gen = outer() gen.next() // -> 'open' gen.next() // -> a generator gen.next() // -> 'close'但如果我們把其中的yield inner()改成yield* inner(),結果又會是什么呢?
var gen = outer() gen.next() // -> 'open' gen.next() // -> 'hello!' gen.next() // -> 'close'事實上,下面兩個function本質上來說是等價的:
function* outer() {yield 'open'yield* inner()yield 'close' }function* outer() {yield 'open'yield 'hello!'yield 'close' }從這個意義上來說,委托的generator函數替代了yield*關鍵字的作用!
這與Co或Koa有什么關系呢?
Generator函數已經很讓人抓狂了,它并不能幫助Koa的generator函數使用Co來控制流程。很多人都會被本地的generator函數和Co框架提供的功能搞暈。
假設有以下generator函數:
function* outer() {this.body = yield inner }function* inner() {yield setImmediatereturn 1 }如果使用Co,它實際上等價于下面的代碼:
function* outer() {this.body = yield co(function inner() {yield setImmediatereturn 1}) }但是如果我們使用yield委托,完全可以去掉Co的調用:
function* outer() {this.body = yield* inner() }那么最終執行的代碼會變成下面這樣:
function* outer() {yield setImmediatethis.body = 1 }每一個Co的調用都是一個閉包,因此它會或多或少地存在一些性能上的開銷。不過你也不用太擔心,這個開銷不會很大,但是如果使用委托yield,我們就可以降低對第三方庫的依賴而從代碼級別避免這種開銷。
有多快?
這里有一個鏈接,是之前我們討論過的有關該話題的內容:https://github.com/koajs/compose/issues/2. 你不會看到有太多的性能差異(至少在我們看來),特別是因為實際項目中的代碼會顯著地降低這些基準。因此,我們并不提倡使用yield* next,不過在內部使用也并沒有壞處。
有趣的是,通過使用yield* next,Koa比Express要快!Koa沒有使用dispatcher(調度程序),這與Express不同。Express使用了許多的調度程序,如connect, router等。
使用委托yield,Koa事實上將:
co(function* () {var start = Date.getTime()this.set('X-Powered-By', 'koa')if (this.path === '/204')this.status = 204if (!this.status) {this.status = 404this.body = 'Page Not Found'}this.set('X-Response-Time', Date.getTime() - start) }).call(new Context(req, res))拆解成:
app.use(function* responseTime(next) {var start = Date.getTime()yield* nextthis.set('X-Response-Time', Date.getTime() - start) })app.use(function* poweredBy(next) {this.set('X-Powered-By', 'koa')yield* next })app.use(function* pageNotFound(next) {yield* nextif (!this.status) {this.status = 404this.body = 'Page Not Found'} })app.use(function* (next) {if (this.path === '/204')this.status = 204 })一個理想的Web application看起來就和上面這段代碼差不多。在上面使用Co的那段代碼中,唯一的開銷就是啟動單個co實例和我們自己的Context構造函數,方便起見,我們將node的req和res對象封裝進去了。
使用它進行類型檢查
如果將yield*應用到不是generator的函數上,你將會看到下面的錯誤:
TypeError: Object function noop(done) {done(); } has no method 'next'這是因為基本上任何具有next方法的東西都被認為是一個generator函數!就我個人而言,我很喜歡這個,因為默認我會假設我就是一個yield* gen()。我重寫了很多我的代碼來使用generator函數,如果我看到某些東西沒有被寫成generator函數,那么我會想,我可以將它們轉換成generator函數而使代碼更簡化嗎?
當然,這也許并不適用于所有人,你也許能找到其它你想要進行類型檢查的原因。
上下文
Co會在相同的上下文中調用所有連續的可yield的代碼。如果你想在yield function中改變調用的上下文,會有些麻煩??聪旅娴拇a:
function Thing() {this.name = 'thing' }Thing.prototype.print = function (done) {var self = thissetImmediate(function () {console.log(self.name)}) }// in koa app.use(function* () {var thing = new Thing()this.body = yield thing.print })這里你會發現this.body是undefined,這是因為Co事實上做了下面的事情:
app.use(function* () {var thing = new Thing()this.body = yield function (done) {thing.print.call(this, done)} })而這里的this指向的是Koa的上下文,而不是thing。
上下文在JavaScript中很重要,在使用yield*時,可以考慮使用下面兩種方法之一:
yield* context.generatorFunction() yield context.function.bind(context)這樣可以讓你避開99%的generator函數的上下文問題,從而避免了使用yield context.method給你帶來的困擾!
本文轉自Jaxu博客園博客,原文鏈接:http://www.cnblogs.com/jaxu/p/6344366.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的yield next和yield* next的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: brk(), sbrk() 用法详解【转
- 下一篇: 跟我学交换机配置(一)