by Groxx · 12y ago · 0 comments
var num_results = 3;
var results = []
var makeCallback = function(index) {
return function(response) {
num_results -= 1;
results[index] = response;
if (num_results > 0) {
return;
}
// actually do stuff here, all results are loaded.
};
};
$.get("/a", makeCallback(0));
$.get("/b", makeCallback(1));
$.get("/c", makeCallback(2));
// etc
It's also not hard to make a generic latch-producing function so you can just write stuff like: var l = makeLatch(callback);
$.get("/a", l());
$.get("/b", l());
$.get("/c", l());Promises are great, I totally agree (as long as they're A+ compliant). But they're not on every platform, and it's not (usually) worth building your own if it's not there.
In your case case, your mutex would be the num_results variable and the conditional is that it is greater than 0.