I'm having trouble getting a queue of deferred objects to call back when complete.
As you can see in the following code, I'm creating a queue (as per this answer: JQuery - $.when syntax for array of Deferred objects) but the answer unfortunately doesn't include the content of the doSomeWork function. Presumably I need to call defer.resolve() as I am doing below.
_getTagLabels: function(tags, callback){
var self = this;
var queue = [];
for (i = 0; i < tags.length; i++) {
var tag = tags[i];
queue.push(new $.Deferred(
function (defer){
self.getLabel(tag.id, function(label) {
tag.label = label;
defer.resolve();
})
}
).promise()
);
}
$.when.apply(undefined, queue).then(function(){
callback();
});
}
However, the callback is never reached. Even though breakpoints on the defer.resolve() are being hit and the tag label being set. I've tried the various other methods of defer, such as done() and then() but none seem to trigger the callback.
EDIT
Here's getLabel and getConcept:
getLabel: function(id, callback) {
var self = this;
self.getConcept(id, function(concept){
callback(concept.label);
});
}
getConcept: function(id, callback){
var self = this;
// abort any current queries.
if (self.getConceptXHR) self.getConceptXHR.abort();
var url = String.format(self.options.conceptTemplate, self.options.serviceEndpointUrl, id);
self.getConceptXHR = $.ajax({
dataType: "jsonp",
url: url,
success: function (response) {
callback(response);
}
});
}