Re:Escape from Callback Hell: Callbacks are the modern goto
by lautis on Friday November 02, 2012 @06:08PM
(#4735003)
Bacon.js[1] is another functional reactive programming library for JavaScript. Many similar concepts to RX.js, but the library is much smaller. While Bacon.js might not be the most mature library around, I've used it successfully on (almost) daily basis for few months.
Re:Escape from Callback Hell: Callbacks are the modern goto
by tlrobinson on Friday November 02, 2012 @03:21PM
(#4734288)
Promises are a good option as well: https://gist.github.com/3889970
Node.js had promises early on, then were removed, but they're slowly gaining traction again.
Re:Escape from Callback Hell: Callbacks are the modern goto
by ikawe on Friday November 02, 2012 @11:21PM
(#4735808)
My understanding is not that promises weren't valued, but that there were conflicting opinions on how to implement them, so promises were left in user land, in hopes that the community would vet a better solution than could be prescribed by the node team.
https://groups.google.com/forum/#!msg/nodejs/jaufClrXU9U/ov5...
q seems to be a popular choice. https://github.com/kriskowal/q
The one I've personally played with is called Arrowlets[1], which introduces a control structure called an arrow that lets you abstract over callbacks and event handling (among other things). Using that style of programming can significantly simplify some fairly common tasks in JavaScript; the drag-and-drop demo on their site is a good motivating example. However, unless you are already familiar with functional programming and arrows, you should probably read some background before diving into the examples.
[1]: http://www.cs.umd.edu/projects/PL/arrowlets/
Another interesting option I've toyed with is RX.js[2]. This is a JavaScript version of C#'s Reactive Extentions (RX). If you are familiar with Linq, then this library's style should seem natural immediately. The core idea here is to abstract over events as streams that can be composed and manipulated conveniently.
[2]: http://rxjs.wikidot.com/
If you don't mind using a different language, but want something that mostly looks like JavaScript, another option is FlapJax[3]. I haven't tried it myself, but I've certainly heard good things about it.
[3]: http://www.flapjax-lang.org/
There are probably more options in the same vein that I forgot or don't know about. However, I think these three are a good starting point and could help clean up much of your event-driven JavaScript code in the short term.
Of course, if you are willing to use a language radically different from JavaScript, then Elm is a great option. Once you get used to functional languages with good type systems, there is really no going back ;). The syntax is also simpler and more minimalistic than JavaScript's, which leads to more readable code.