6 ms·
Three Routes to Spaghetti-Free Javascript
- nanoanderson 16y agoNot sure if the title is indicative of the tutorial's true usefulness. The information on asynchronous function calling alone is worth the click-through. I wasn't aware of any of the three javascript packages (are they libraries? frameworks? seems like they're not really), and anyone looking to improve their asynchronous javascript writing skills and readability should take a look. The author admits that debugging could be an issue since these packages all generate new javascript from your current code, but the concepts look very promising.
- jdp23 16y agoAgreed. Extremely interesting and useful ... and not at all what I expected from the title!
- bambax 16y agoBut why isn't jQuery 1.5 mentionned? See this: http://www.erichynds.com/jquery/using-deferreds-in-jquery/ http://www.erichynds.com/jquery/using-deferreds-in-jquery/
- treeface 16y agoYes, exactly. This is a great description of the new deferred events and methods.
- defeated 16y agoI've written a library for internal use (that I'm waiting for work to agree to open source) that basically lets you bundle a group of asynchronous calls into a single callback. I prefer this method because it doesn't try and hide the asynchronous nature of the code, it just frees you up from having to nest your callbacks N deep when you require the output from all N asynchronous operations to continue. Wondering what others think about this? Do folks prefer the methods in this article that let you hide the asynchronous nature of some code?
- stdbrouw 16y agoIt's been done. One of the best libraries for managing asynchronous code in JS without hiding it away is http://github.com/caolan/async http://github.com/caolan/async I agree wholeheartedly, by the way. There's way too much scope for leaky abstractions when you try to hide away the true nature of asynchronous code. Same reason, I think, why most people are lukewarm about using continuations in web development, like e.g. Seaside does: in principle, very neat, very clean, but it hides away a fundamental aspect of how the web works, which can lead to all sorts of unforeseen behavior re. caching, debugging et cetera.
- defeated 16y agoYeah, I assumed there had to be libraries to do this already, but didn't come across anything that did what I wanted. I'll check out async though, thanks for the heads up.
- Detrus 16y agoThere are many many attempts at solving this problem, both libraries and preprocessors. None seem to satisfy a large audience, most people seem to deal with the callback nesting and hope to avoid cases where it gets super ugly. I have a lot bookmarked on github, these are mostly libraries. https://github.com/coolaj86/futures https://github.com/coolaj86/futures https://github.com/substack/node-seq https://github.com/substack/node-seq https://github.com/marcuswestin/fun https://github.com/marcuswestin/fun https://github.com/creationix/step https://github.com/creationix/step https://github.com/chriso/chain.js https://github.com/chriso/chain.js https://github.com/substack/node-chainsaw https://github.com/substack/node-chainsaw https://github.com/caolan/async https://github.com/caolan/async https://github.com/kriszyp/promised-io https://github.com/kriszyp/promised-io https://github.com/kriszyp/node-promise https://github.com/kriszyp/node-promise https://github.com/technoweenie/node-chain-gang https://github.com/technoweenie/node-chain-gang https://github.com/creationix/do https://github.com/creationix/do https://github.com/willconant/flow-js https://github.com/willconant/flow-js https://github.com/creationix/conductor https://github.com/creationix/conductor Some branches of CoffeeScript are also trying to ease the pain. Defer keyword didn't make it into CoffeeScript main. My impression is CoffeeScript makes callback hell less ugly anyway, so there is less need for adding a complex language feature that generates unreadable JavaScript.
- deleted 16y ago[deleted]
- astrofinch 16y agoCould we make getTitle a method of some webpage object? Then the event handler could set the webpage instance's title attribute. Webpages could have class variables that kept track of the total number of webpages created and the total number that had titles.
- deleted 16y ago[deleted]
- Groxx 16y agoRoute 1: Streamline.js, a compiles-to-Javascript language. Route 2: Mobl, a compiles-to-Javascript language. Route 3: Stratified.js, a compiles-to-Javascript language. And then a side-mention for something actually in Javascript: https://github.com/caolan/async https://github.com/caolan/async So, to make spaghetti-free Javascript... don't write Javascript? I'll keep looking, thanks.
- treeface 16y agoTry jQuery's deferred events. It is, IMO, the most elegant actual-JavaScript solution to the problem I've seen.
- pcwalton 16y agoA colleague of mine wrote a true coroutine library for JavaScript: https://github.com/dherman/jstask https://github.com/dherman/jstask It depends on generators, so it works only in Firefox, but it's a cool hack nonetheless, and might be useful if you're developing Firefox addons.
- Zef 16y agoI think many people missed my main point. Here's another shot at explaining it better: http://zef.me/3753/planning-ahead-the-async-javascript-problem http://zef.me/3753/planning-ahead-the-async-javascript-probl...