Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable harmony syntax support in coffeescript?

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?

like image 751
let4be Avatar asked Jul 11 '14 14:07

let4be


People also ask

Is CoffeeScript obsolete?

In summary, CoffeeScript began as a fantastic idea (making it easier to write JavaScript code) ultimately, however, it did not stand the test of time and was pushed out by JavaScript and currently, hardly anyone remembers about it.

Is CoffeeScript similar to Python?

CoffeeScript is a programming language whose syntax is clearly designed to match much of Python (with additional inspirations from Perl, ECMAScript, Ruby, etc.).


1 Answers

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

like image 197
let4be Avatar answered Sep 20 '22 08:09

let4be