As someone who has made an MVC framework in Node.js and launched a production site using that framework, I can assure you that it's not hard to avoid async callback mess. I can also assure you that many others have had success doing this.
It's really not difficult to write beautiful, modular, and readable code in node.js without going past 80 columns and without using Iced CoffeeScript.
I suspect that comments like this only further reinstate the theory that most people are dismissing Node primarily due to the annoying amount of hype. There are better reasons to not use Node, but imo, this isn't a very strong argument.
I'd love to see this framework you wrote. Is it open-source? or is it something you're unable to share?
$data = getData();
And people want to substitute the return for a callback in js. getData(function(data) {
// Do something with data.
});
That indeed does lead to callback mess. But when you are coding in js you rarely should use anonymous functions. I mostly just use them when I might want to perform recursion. Going back to my example, I would write the operation as an object like so: var SomeObject = {
start: function() {
getData(dataGot);
},
dataGot: function(data) {
}
};
JavaScript is an object-oriented languages; use objects! They make async painless.In the example above, you're unable to use ''this'' in the ''dataGot'' function (when invoked as a callback).
Object orientedness is very easy to get wrong in JavaScript, if you're coming from C++/Java/Python background.
I also forgot that it's this.dataGot. dataGot doesn't exist in that context :)
I'd still end up with this, thanks to callbacks.
app.get('/documents', function(req, res) {
Document.find().all(function(documents) {
// 'documents' will contain all of the documents returned by the query
res.send(documents.map(function(d) {
// Return a useful representation of the object that res.send() can send as JSON
return d.__doc;
}));
});
});
Seems to me that what's missing is something that lets me write it in a more simple way. What I have here looks like an absolute nightmare of coupling. So if you have some suggestions you could point me to that show a little more detail, I'd be really grateful. app.get('/documents', new DocumentRequest);Doesn't that seem to imply that it is such a problem that people have come up with a myriad of disparate solutions to try to solve it?
I agree that it is a problem if neglected, but most people don't allow it to be a problem in the first place.
It's like knocking on PHP because it could be used for templating and business logic on the same file. Just because you could, doesn't mean you have to (or should).
I honestly don't feel that it's as big of a problem as people make it out to be. It's simply a minor annoyance. The error handling is the killer.
$(ctx).geturl("http://example.com).parseExample().renderTemplate().end();
Now this is a bit different from jQuery since all the methods here do is push method names and arguments onto a stack. it's the .end() method that reads in the stack and goes through and executes it. This means that each method along the way we've constructed a partial computation value- that is, the stack, which we can pass around as a value- just leave off the "end" method...
var scrapeHN = $(ctx).geturl("http://example.com).parseExample().renderTemplate();
now we can modify the computation by turning it into an error handling monad...
var ecm = ErrorCatchingMonad(scrapeHN, errorCallback);
this wraps each method in the monad with a try catch block, and calls the callback if an error gets thrown, and stops the chain from getting executed further. in fact this is how the origial monad was created
var $ = DeferringMonad(scrapingAPI);
which takes a normal jquery style plugin interface/chaining api and turns it into that deferring stack type api with an end(); method.
real answer: It's easy and fun to whine about things you don't get in a language/paradigm you don't know. So easy and fun in fact that's almost entirely what programmers talk about to each-other. But whining hardly gets anything done, does it. The answer is: It's not a problem if you know what you're doing. And that applies to all other instances of this programmer discourse pattern.