Created
January 28, 2010 17:11
-
-
Save creationix/288931 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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