Skip to content

Instantly share code, notes, and snippets.

@creationix
Created January 28, 2010 17:11
Show Gist options
  • Save creationix/288931 to your computer and use it in GitHub Desktop.
Save creationix/288931 to your computer and use it in GitHub Desktop.
// combo library
function Combo(callback) {
this.callback = callback;
this.items = 0;
this.results = [];
}
Combo.prototype = {
add: function () {
var self = this;
this.items++;
return function () {
self.check(self.items - 1, arguments);
};
},
check: function (id, arguments) {
this.results[id] = arguments;
this.items--;
if (this.items == 0) {
this.callback.apply(this, this.results);
}
}
};
// Usage
var both = new Combo(function () {
puts(inspect(arguments));
});
setTimeout(both.add(), 100);
setTimeout(both.add(), 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment