我怎样才能强制在Coffeescript 1.9使用生成器?

CoffeeScript支持生成器现在,但是,我发现只有你使用yield关键字,那么你的函数将被编译为生成器,这里是我的问题,我用koa写我的代码,而一些中间件我不需要asynchronous逻辑,所以我不CoffeeScript认为这是一个正常的function,但是, koa说: app.use() requires a generator function T ^ T,任何人都有一个解决scheme? 谢谢!

使用武力,卢克! ;)

我把这个例子从主页移植到了CoffeeScript,并且简单地让最后一个处理程序也接受next参数并yield它,尽pipe这完全没有必要。

变成它工作得很好。 然而, yield null是行不通的。

 koa = require("koa") app = koa() # x-response-time app.use (next) -> start = new Date yield next ms = new Date - start @set 'X-Response-Time', ms + 'ms' # logger app.use (next) -> start = new Date yield next ms = new Date - start console.log '%s %s - %s', this.method, this.url, ms # response app.use (next) -> @body = "Hello World" yield next app.listen(3000) 

正如在这个coffeescript问题中所描述的,你可以使用yield return来强制这个函数成为一个生成器。 我知道这不是很好,但似乎是目前唯一的select。