News for hackers. Stuff that matters.
Re:Callbacks are Pretty Okay by prezjordan on Saturday August 17, 2013 @02:53AM (#6228397)
Right, but now your functions are very tightly coupled to each other. The indentation might be nicer, but functionally it's just misleading.

The cool thing about promises is that your methods are pretty portable. For example:

    function getUserFriends(userName, next) {
        db.users.findOne({name:userName}, foundOne);

        function foundOne(err, user) {
            if (err != null) return next(err);
            db.friends.find({userId:user.id}, foundFriends);
        }

        function foundFriends(err, friends) {
            if (err != null) return next(err);
            return next(null, friends);
        }
    }
Can be rewritten (using q[0]) as:

    var Q = require('q');

    function getUserFriends(userName, next) {
      var deferred = Q.defer();
      db.users.findOne({name:userName}, deferred.makeNodeResolver());
      return deferred.promise;
    }

    function foundOne(user) {
      var deferred = Q.defer();
      db.friends.find({userId:user.id}, deferred.makeNodeResolver());
      return deferred.promise;
    }

    function foundFriends(friends) { ... }

    getUserFriends.then(foundOne).then(foundFriends);
You'll see here that getUserFriends, foundOne, and foundFriends (which I left out) are now all portable and can be used elsewhere in the code for other purposes (because they are no longer tightly coupled).

[0]: https://github.com/kriskowal/q

Re:Callbacks are Pretty Okay by StavrosK on Saturday August 17, 2013 @03:49AM (#6228484)
With the downside that I have absolutely no clue what this code does or how.
Re:Callbacks are Pretty Okay by DrJokepu on Saturday August 17, 2013 @04:00AM (#6228502)
Once you get used to promises (it took me a couple of days) it's not confusing at all. It takes a bit of effort though. I remember when I first started programming none of the programs made any sense to me; as you said, I had absolutely no clue what the code did and how. But then I kept practicing and soon I understood how things work.
Re:Callbacks are Pretty Okay by StavrosK on Saturday August 17, 2013 @07:30AM (#6228804)
Yeah, I guess it's not fair for me to compare something I'm familiar with to something I'm not. I'm sure promises are easy to understand, but that API there does not look great.
Re:Callbacks are Pretty Okay by modiho on Saturday August 17, 2013 @05:27AM (#6228626)
Q has some utility functions around wrapping node style callbacks that make the code a bit shorter and arguably more easy to understand.

    var Q = require('q');

    function getUserFriends(userName) {
      return Q.nfcall(db.users.findOne, {name:userName});
    }

    function foundOne(user) {
      return Q.nfcall(db.friends.find, {userId:user.id});
    }

    function foundFriends(friends) { ... }

    getUserFriends.then(foundOne).then(foundFriends);
Re:Callbacks are Pretty Okay by prezjordan on Saturday August 17, 2013 @02:52PM (#6229922)
That's much better, thanks for sharing