简体   繁体   English

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

[英]How to enable harmony syntax support in coffeescript?

I used node.js(0.11.13) with --harmony flag and used function *() and yield keywords. 我用的node.js(0.11.13) --harmony标志,并使用function *()yield关键字。

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' . 我试图在coffeescript的帮助下简化我在node.js上的开发,到目前为止它工作得很好但是我遇到了带有yield麻烦并且声明了一个生成器 - 它抱怨'保留关键字产量'

Any ideas? 有任何想法吗?

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}`)()

It's seems to be perfectly valid coffee-script, though, webstorm cofeescript plugin cries about errors, but it works. 它似乎是完全有效的咖啡脚本,但是,webstorm cofeescript插件会对错误发出呐喊,但它确实有效。

Also the following solution(vanilla coffeescript and gulp) is possible: 以下解决方案(香草coffeescript和gulp)也是可能的:

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)

magic: no errors in IDE :) 魔术:IDE中没有错误:)

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 更新在尝试并阅读了很多有关咖啡,ecma6及其未来的内容之后,我决定放弃coffeescript并使用ECMA6支持node.js和客户端的traceur

It's actually now possible to use yield in coffeescript 1.9.x 实际上现在可以在coffeescript 1.9.x中使用yield

from coffeescript's website 来自coffeescript的网站

CoffeeScript functions also support ES6 generator functions through the yield keyword. CoffeeScript函数还通过yield关键字支持ES6生成器函数。 There's no function*(){} nonsense — a generator in CoffeeScript is simply a function that yields. 没有function*(){}无意义 - CoffeeScript中的生成器只是一个产生的函数。

example: 例:

perfectSquares = ->
  num = 0
  loop
    num += 1
    yield num * num
  return

Use (for example) my fork: https://github.com/xixixao/coffee-script 使用(例如)我的分支: https//github.com/xixixao/coffee-script

There are other ones with different syntax. 还有其他语法不同的。

Generators support (the yield keyword) landed in the master branch some time ago, but haven't been released yet. 生成器支持(yield关键字)不久前登陆了主分支,但还没有发布。

You can get it via NPM and put as a dependency in package.json: 您可以通过NPM获取它并将其作为依赖项放在package.json中:

npm i jashkenas/coffeescript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM