Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It <em>is</em> a large problem with Node-based code; you frequently grow "callback pyramids". There are several approaches to dealing with the problem:</p> <p><strong>Code style:</strong></p> <p>Use this annoyance as an opportunity to break your code into bite sized chunks. It means you're likely going to have a proliferation of tiny named funcs - that's probably just fine, though! You might also find more opportunities for reuse.</p> <p><strong>Flow-control Libraries</strong></p> <p>There are exactly 593.72 billion flow control libraries out there. Here's some of the more popular ones:</p> <ul> <li><a href="https://github.com/creationix/step" rel="nofollow">Step</a> super basic serial &amp; parallel flow management.</li> <li><a href="https://github.com/substack/node-seq" rel="nofollow">seq</a> is a heavier but more feature-full flow control library.</li> <li>There's plenty more. Search <a href="http://search.npmjs.org/" rel="nofollow">the npm registry</a> for "flow" and "flow control" (sorry, doesn't appear to be linkable)</li> </ul> <p><strong>Language Extensions</strong></p> <p>There are several attempts to provide a more synchronous-feeling syntax on top of JavaScript (or CoffeeScript), often based on the concepts behind the <a href="http://pdos.csail.mit.edu/~max/docs/tame.pdf" rel="nofollow">tame paper</a>.</p> <ul> <li><a href="http://tamejs.org/" rel="nofollow">TameJS</a> is the OkCupid team's answer to this.</li> <li><a href="http://maxtaco.github.com/coffee-script/" rel="nofollow">IcedCoffeeScript</a> they've also ported TameJS over CoffeeScript as a fork.</li> <li><a href="https://github.com/Sage/streamlinejs" rel="nofollow">streamline.js</a> is very similar to TameJS.</li> <li><a href="http://onilabs.com/stratifiedjs" rel="nofollow">StratifiedJS</a> is a heavier approach to the problem.</li> </ul> <p>This route is a deal-breaker for some:</p> <ul> <li>It's not standard JavaScript; if you are building libraries/frameworks/etc, finding help will be more difficult.</li> <li>Variable scope can behave in unexpected ways, depending on the library.</li> <li>The generated code can be difficult to debug &amp; match to the original source.</li> </ul> <p><strong>The Future:</strong></p> <p>The node core team is very aware of the problem, and are also working on lower level components to help ease the pain. It looks like they'll be introducing <a href="https://groups.google.com/d/msg/nodejs/eVBOYiI_O_A/1ShfZZvb_4QJ" rel="nofollow">a basic version of domains</a> in v0.8, which provide a way of rolling up error handling (avoiding the common <code>return err if err</code> pattern, primarily).</p> <p>This should start to lay a great foundation for cleaner flow control libraries, and start to pave the way for a more consistent way of dealing with callback pyramids. There's too much choice out there right now, and the community isn't close to agreeing on even a handful of standards yet.</p> <p><strong>References:</strong></p> <ul> <li><a href="http://book.mixu.net/ch7.html" rel="nofollow">Mixu's Node book has an awesome chapter</a> on this subject.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload