I am having a problem of executing Deferred objects sequentially. After completion of one, another should start.
My function is like this :
delete_items:function(objects,type){
var deferred = $.Deferred(),
deletePromises = [], //Store All deferred objects in this array.
success = [], //if success push object in this array
fail = [], //if failure push objects in this array
objects.forEach(function (object) {
//pushing the objects in the array
deletePromises.push(
del(object.id) // this is a deferred function.
.done(function (){
success.push(object);
}).fail(function () {
fail.push(object);
}));
});
//executing them but this doesn't wait for one object to complete before executing another
$.when.apply(this, deletePromises).done(function () {
deferred.resolve(success);
}).fail(function (reason) {
deferred.reject(reason, "DELETE");
});
return deferred.promise();
}
I want the objects in the deletePromises[]
to execute according to their index and when one object returns success or failure then next object in this array should be executed.
Any help would be great.