Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'd caution against any "I'd go further to say" statements. Either you know because you've tried, or you don't and are simply guessing.

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.



All the examples, github repos, books, and tutorials I'm seeing with my dive into node say exactly the opposite - the code becomes a mess of callbacks. It looks like enormous work goes into avoiding callbacks, similar to the amount of work that went into Rails in 2005 so that server-side folks could avoid writing JavaScript.

I'd love to see this framework you wrote. Is it open-source? or is it something you're unable to share?


You have to think about async code differently than you think about synchronous code. In PHP you might write:

  $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.


Wrapping things in objects doesn't seem to help me.

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.


I would create a DocumentRequest object that takes your req and res, then break apart your callbacks to separate functions on the DocumentRequest prototype. Then you would simply consume it with:

  app.get('/documents', new DocumentRequest);


Thanks for this. Been thinking about this. Could you recommend a book on patterns like these?


> 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.


Right, I excluded the bind for the sake of simplicity.

I also forgot that it's this.dataGot. dataGot doesn't exist in that context :)


Your assumption that I have no experience, and have therefore based my statement on hearsay is incorrect.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: