title: How to avoid long nesting of asynchronous functions in Node.js
body: <p>I want to make a page that displays some data from a DB, so I have created some functions that get that data from my DB. I'm just a newbie in Node.js, so as far as I understand, if I want to use all of them in a single page (HTTP response) I'd have to nest them all:</p>
  
  <pre><code>http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var html = "&lt;h1&gt;Demo page&lt;/h1&gt;";
    getSomeDate(client, function(someData) {
      html += "&lt;p&gt;"+ someData +"&lt;/p&gt;";
      getSomeOtherDate(client, function(someOtherData) {
        html += "&lt;p&gt;"+ someOtherData +"&lt;/p&gt;";
        getMoreData(client, function(moreData) {
          html += "&lt;p&gt;"+ moreData +"&lt;/p&gt;";
          res.write(html);
          res.end();
        });
      });
    });
  </code></pre>
  
  <p>If there are many functions like that, then <strong>the nesting becomes a problem</strong>.</p>
  
  <p>Is there a way to avoid this? I guess it has to do with how you combine multiple asynchronous functions, which seems to be something fundamental.</p>