5

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

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'.

Any ideas?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
let4be
  • 1,048
  • 11
  • 30
  • After 4 hours of research I found that original coffeescript have stucked for years at adding yield support... I see only some blackmagic ways like putting the whole function body in pure js with ``, or using https://github.com/paiq/blackcoffee... so sad :( and hax yield support with superior power of macros... – let4be Jul 11 '14 at 18:58
  • 2
    And I really frightened to use this in production... It feels like blackmagic being ruled by another even more powerful blackmagic. – let4be Jul 11 '14 at 19:13

4 Answers4

5

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.

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)

magic: no errors in 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

let4be
  • 1,048
  • 11
  • 30
4

It's actually now possible to use yield in coffeescript 1.9.x

from coffeescript's website

CoffeeScript functions also support ES6 generator functions through the yield keyword. There's no function*(){} nonsense — a generator in CoffeeScript is simply a function that yields.

example:

perfectSquares = ->
  num = 0
  loop
    num += 1
    yield num * num
  return
kdabir
  • 9,623
  • 3
  • 43
  • 45
3

Use (for example) my fork: https://github.com/xixixao/coffee-script

There are other ones with different syntax.

xixixao
  • 481
  • 6
  • 16
  • This does not solve the problem with broken highlighting in webstorm. And I do not understand how to use forks via npm – let4be Jul 12 '14 at 17:55
  • 1
    Generators are supported by master. To use forks/master branch via npm use `npm install jashkenas/coffee-script` (the instructions are in the README of my fork as well, but that one is now obsolete). – xixixao Sep 30 '14 at 21:16
1

Generators support (the yield keyword) landed in the master branch some time ago, but haven't been released yet.

You can get it via NPM and put as a dependency in package.json:

npm i jashkenas/coffeescript
Tobias Cudnik
  • 9,240
  • 4
  • 24
  • 18