CoffeeScript 1.9 has finally introduced support for long awaited generators, which promise to prevent callback hell and help writing async code.
Generators are, in short functions which can be exited and later re-entered, guaranteeing that their variable bindings will be saved across re-entrances. CoffeeScript 1.9 defines a generator as any function that uses the new yield
keyword. This is an example of a simple generator:
foo = ->
i = 0
yield i +1
yield i +2
Calling repeated time the generator will provide the yielded values in succession, that is 1, then 2.
Generators are part of the ECMAScript 6 proposal and have been previously introduced in node 0.11, although they are enabled only when you pass the --harmony-generators
flag to node. Generators have been shown to provide an alternative model to callbacks for handling async code, either used in conjunction with promises or not.
Other features provided by CoffeeScript 1.9 are:
- Improved parsing and error reporting for strings and regexs.
- New strategy to generate internal compiler variable names implying that parameters declared as
@parameter
are no longer available asexample
within the function body. - Fixed REPL compatibility with latest version of Node and lo.ja.
CoffeeScript is a language that compiles into JavaScript with the aim to expose the good parts of JavaScript in a simple way.