I have a simple service method that gather several .get()
and I'm having some troubles on the "printing" part as by that time I only have one part of the result.
what I'm doing is:
var service = function() {
var players = []; // will hold 100 objects
var getMembers = function(id) {
$.get(url, function(data) {
for(i=0; i<data.length; i++) {
var p = data[i];
// get more info for this member
getMemberDetails(p.member_id);
// put the current data into the players
players.push(p);
}
});
calculateAndPrint();
};
var getMemberDetails = function(id) {
$.get(url, function(data) {
var result = $.grep(players, function(e){ return e.member_id == id; });
if (result.length == 0) { /* not found */ }
else if (result.length == 1) {
// push new data to player object
result[0].details = data;
}
});
};
var calculateAndPrint = function() {
for(i=0; i<players.length; i++) {
var p = players[i];
console.log(p);
}
};
})();
and this does not work, as when I reach calculateAndPrint
, the details
is not even designed yet...
so I tried $.Deferred()
and the only issue I'm having is that if I defer getMemberDetails
method, that call includes already a deffer call (the .get()
) and I'm back to the same issue ...
what is the best option to only run calculateAndPrint
after all 100 calls were made?
It seems easy enough but I'm just blank :/