如何在 Coffeescript 1.9 上强制使用生成器?

How can I force use generator on Coffeescript 1.9?

CoffeeScript 现在支持生成器,但是,我发现只有你使用 yield 关键字,然后你的函数将被编译为生成器,这是我的问题,我使用 koa 来编写我的代码,和一些中间件我不需要异步逻辑,所以我不需要 yield,所以,CoffeeScript 认为这是一个正常的功能,但是,koa 说:app.use() requires a generator function,T^T ,有人有解决办法吗?谢谢!

使用原力,卢克! ;)

我将 example from the homepage 移植到 CoffeeScript,并简单地让最后一个处理程序也接受一个 next 参数和 yield 给它,尽管这完全没有必要。

原来它工作得很好。但是,执行 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)

this coffeescript issue中所述,您可以使用yield return强制函数成为生成器。 我知道这不太好,但它似乎是目前唯一的选择。