<i>"Asynchronous event-driven programming is at the heart of Node.js, however it is also the root cause of Callback Hell."</i><p>I'd argue the root cause is... callbacks.<p>Asynchronous programming can be done elegantly, in a synchronous style using "async/await", originally (?) in C# [1], likely to be added to the next version of JavaScript [2], also in Dart [2], Hack [4], and Python 3.5 [5]. It can also be emulated in languages with coroutines/generators [6][7][8] (which in turn can be implemented by a fairly simple transpiler [9][10])<p>This:<p><pre><code> function foo(a, callback) {
bar(a, function(err, b) {
if (err) {
callback(err)
} else {
baz(b, function(err, c) {
if (err) {
callback(err)
} else {
// some more stuff
callback(null, d)
}
})
}
})
}
</code></pre>
Becomes this:<p><pre><code> async function foo() {
var a = await bar(a)
var c = await baz(b)
// some more stuff
return d;
}
</code></pre>
And you'll see even greater improvements when using other constructs like try/catch, conditionals, loops, etc.<p>[1] <a href="https://msdn.microsoft.com/en-us/library/hh191443.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/hh191443.aspx</a><p>[2] <a href="http://jakearchibald.com/2014/es7-async-functions/" rel="nofollow">http://jakearchibald.com/2014/es7-async-functions/</a><p>[3] <a href="https://www.dartlang.org/articles/await-async/" rel="nofollow">https://www.dartlang.org/articles/await-async/</a><p>[4] <a href="http://docs.hhvm.com/manual/en/hack.async.asyncawait.php" rel="nofollow">http://docs.hhvm.com/manual/en/hack.async.asyncawait.php</a><p>[5] <a href="https://lwn.net/Articles/643786/" rel="nofollow">https://lwn.net/Articles/643786/</a><p>[6] <a href="https://github.com/petkaantonov/bluebird/blob/master/API.md#promisecoroutinegeneratorfunction-generatorfunction---function" rel="nofollow">https://github.com/petkaantonov/bluebird/blob/master/API.md#...</a><p>[7] <a href="https://github.com/kriskowal/q/tree/v1/examples/async-generators" rel="nofollow">https://github.com/kriskowal/q/tree/v1/examples/async-genera...</a><p>[8] <a href="http://taskjs.org/" rel="nofollow">http://taskjs.org/</a><p>[9] <a href="https://babeljs.io/docs/learn-es6/#generators" rel="nofollow">https://babeljs.io/docs/learn-es6/#generators</a><p>[10] <a href="https://facebook.github.io/regenerator/" rel="nofollow">https://facebook.github.io/regenerator/</a>