如何在 coffeescript 中启用和声语法支持?

我使用带有 --harmony 标志的 node.js(0.11.13) 并使用了 function *()yield 关键字.

I used node.js(0.11.13) with --harmony flag and used function *() and yield keywords.

我尝试借助 coffeescript 简化我在 node.js 上的开发,到目前为止它运行良好,但我遇到了 yield 和声明生成器的麻烦 - 它抱怨 'reserved关键字产量'.

I tried to simplify my development on node.js with help of coffeescript, so far it works great but I went into troubles with yield and declaring a generator - it complains about 'reserved keyword yield'.

有什么想法吗?

推荐答案

另一种打开黑色次元之门的方法是:

Another way to open the gate to the black dimension is:

co = require 'co'
sleep = require 'co-sleep'

co(`function*(){1`
    console.log 'hi!'
    `yield sleep(1000)`
    console.log 'bye!'
`1}`)()

这似乎是完全有效的咖啡脚本,不过,webstorm cofeescript 插件会抱怨错误,但它可以工作.

It's seems to be perfectly valid coffee-script, though, webstorm cofeescript plugin cries about errors, but it works.

以下解决方案(vanilla coffeescript 和 gulp)也是可能的:

Also the following solution(vanilla coffeescript and gulp) is possible:

co      = require 'co'
sleep   = require 'co-sleep'
$       = (cor) -> cor
$yield  = (cor) -> cor

do co $ ->
    console.log "hi!"
    $yield sleep(1000)
    console.log "bye!"

gulp.task 'node-js', ->
    gulp.src config.srcServerJs, {base: config.srcServerJsBase}
    .pipe plumb()
    .pipe coffee()
    .pipe replace(/$(function(/g, '$(function*(')
    .pipe replace(/$yield(/g, 'yield (')
    .pipe gulp.dest(config.dstServerJs)

魔术:IDE 中没有错误 :)

magic: no errors in IDE :)

更新在尝试并阅读了很多关于咖啡、ecma6 及其未来的资料后,我决定放弃咖啡脚本并使用 ECMA6 支持 node.js 和客户端的 traceur

Update After trying and reading a lot of stuff about coffee, ecma6 and its future I decided to give up on coffeescript and go with ECMA6 with support of traceur for both node.js and client-side

相关文章