nanha
11/15/2011 - 5:00 AM

Async Programming Combo

Async Programming Combo

function Combo(callback) {
  this.callback = callback;
  this.items = 0;
  this.results = [];
}

Combo.prototype = {
  add: function () {
    var self = this,
        id = this.items;
    this.items++;
    return function () {
      self.check(id, arguments);
    };
  },
  check: function (id, arguments) {
    this.results[id] = Array.prototype.slice.call(arguments);
    this.items--;
    if (this.items == 0) {
      this.callback.apply(this, this.results);
    }
  }
};

// Make a Combo object.
var both = new Combo(function (db_result, file_contents) {
  // Do something
});
// Fire off the database query
people.find({name: "Tim", age: 27}, both.add());
// Fire off the file read
fs.readFile('famous_quotes.txt', both.add());