ToffeeScript – CoffeeScript with async syntax and some additional features
github.com
github.com
Smarter coding does not fix the callback pyramid either, because then your callback logic is spread through the source and not where it belongs, making things even worse!
And when your callbacks depend on each other it's REAL HELL, because it's easy to create a tangle of mutable closed variables far from the real logic which IMHO is not a very good pattern. I tend to prefer functional programming with pure functions, but callback-hell-code can quickly resemble the worst parts of OOP if you're not careful (without the good parts such as encapsulation).
I love Node.js, it's my platform of choice, but big projects can quickly get out of control. I'm really looking forward to generators to fix the issue.
I couldn't understand callback hell either, until I really got into Node.js programming for big projects.
[0] including a module system, fork-join parallelism, try/catch and stacktrace support even for async code.
(disclaimer: I joined Oni Labs to work on SJS, after struggling with my own coffeescript fork for 6 months)
The generated code is using callbacks. The point is that instead of writing something like (a cleaner, but still ugly, version of) the generated code on the right, you’d instead write the (quite easy to read) toffeescript code on the left.
That said, I think coffeescript’s “it’s just javascript” slogan (and goal of generating readable code) is one of its most important features. It has to be quite a bit harder to debug than the coffeescript version, since the generated code has control flow that’s so different looking from the input.
That explains the many ways to structure your async code in pure JavaScript (no helper libraries or transpilers) so you don't create callback hell code.
As an analogy, consider what would happen if Outlook was written in VBA and you opened OUTLOOK.DOC in Word to read your email. Well that's GMail.
With good tools and a sufficiently correct compiler, that should be as irrelevant as worrying that compiled x86 assembly is harder to read than the C source.
I'll need to see more real world examples to decide if Toffee's async syntax is better than Iced.
var x, y, _this = this;
a(b, function() { x = arguments[0], y = arguments[1]; return console.log(x, y); }); ReferenceError: a is not defined
Anything on the left-hand side of the assignment (in this case, x and y) are arguments passed to this anonymous function. A concrete example of why you would do this would probably be useful.
err, data = fs.readFile! path
{
if (err) throw err;
console.log data
}