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:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var html = <h1>Demo page</h1>;
getSomeDate(client, function(someData) {
html += <p>+ someData +</p>;
getSomeOtherDate(client, function(someOtherData) {
html += <p>+ someOtherData +</p>;
getMoreData(client, function(moreData) {
html += <p>+ moreData +</p>;
res.write(html);
res.end();
});
});
});
If there are many functions like that, then the nesting becomes a problem.
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.