Here is a simple library that may help:
// 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);
For use with node promises, you would simply pass `both.add()` to the addCallback functions. When everything is done, your initial callback will be called with the results ordered in the order you registered them, not the order they actually came.
> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>