CoffeeScript 现在支持生成器,但是,我发现只有你使用 yield
关键字,那么你的函数才会被编译为生成器,这是我的问题,我使用 koa
来编写我的代码,有些中间件我不需要异步逻辑,所以我不需要yield
,所以,CoffeeScript认为这是一个正常的函数,但是,koa
说: app.use() 需要一个生成器函数
,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)
关于node.js - 如何在 Coffeescript 1.9 上强制使用生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466732/