I have a variable amount of $.Deferred()
objects that are getting pushed into an array for execution via $.when()
. Using suggestions from elsewhere on Stack Overflow, I decided to go with the hand-coded $.when.all([$.Deferred(), ...])
method.
To be specific, the $.Deferred()
objects contain a function that executes one indexedDB .put()
per object. I would expect them to complete upon calling $.when.all()
, and execute code within it's corresponding .then()
function, yet I discovered via breakpoints that it immediately executes the .put()
upon invoking it, and the execution of the javascript bypasses the .then()
function all together and just continues on.
var putQueryArray = [];
_.each(favorites, function(fav) { // favorites being an array of updated indexedDB .get()s
var objectStore = db.transaction(['store'], 'readwrite') // var db defined elsewhere
.objectStore('store');
putQueryArray.push($.Deferred(function() {
var update = objectStore.put(fav); // This executes immediately, as verified in Chrome's Resources tab
update.onsuccess = _.bind(function(e) {
this.resolve(e); // Even if I put a breakpoint here, it never triggers
}, this);
update.onerror = _.bind(function(e) {
this.reject(e);
}, this);
}).promise());
});
// It appears this is ignored all together
$.when.all(putQueryArray).then(function(res) { // I (think I) expect this line to execute the above array of $.Deferred()'s?
//This function never executes
}, function(err) { ... });
I've tried everything from not wrapping the entire function in $.Deferred(function() { ... });
, instead calling $.Deferred as a variable within the function and returning its promise, to excluding the .promise()
part all together, both of which result in it actually executing the .then()
function, but not actually executing the .put()
. What am I doing wrong in the above code?