2

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.

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
  • 2
    it would be helpful if you had a fiddle. But this idiom might be useful. http://stackoverflow.com/questions/20308641/iterate-over-indefinite-array-of-deferred-items/20326123#20326123 – Sumit Mar 21 '14 at 20:41
  • this looks good. i will try. Thanks for suggestion. never thought this way. –  Mar 21 '14 at 20:52

0 Answers0