1

我正在尝试使用 Deferred 正确排序对 API 的删除调用。该应用程序处理项目和图像:

Item:
ItemId

Images:
ImageId
ItemId

所以在删除与之关联的项目之前必须删除图像,否则我的 DeleteItem() ajax 调用将在数据对象中返回错误。

在下面的代码中,我试图在进行任何 DeleteItem() ajax 调用之前对所有 DeleteImage() ajax 调用进行分组。

var deferreds1 = [];  
var deferreds2 = [];   

...
// push()ing an unknown number of DeleteImage() ajax calls to deferred1 
// push()ing an unknown number of DeleteItem() ajax calls to deferred2
...

if (deferreds1.length > 0) {
    $.when
        .apply($, deferreds1)
        .then(function(){
             console.log('deferred1 then');
             $.when
                  .apply($, deferreds2)
                  .then(function() {
                       console.log('deferred2 then');
                  })
                  .fail(function() {
                       console.log('deferred2 fail');
                  });
             })
         .fail(function(){
              console.log('deferred1 fail');
         });
}

function DeleteItemImage() {
    var dfd = $.Deferred();

    $.ajax({
        ...
        success: function (data) {
            if (!data.success) {
                alert('error');
            }
            dfd.resolve();
        }
        ...
    }); 

    return dfd.promise();
}

function DeleteItem() {
    var dfd = $.Deferred();

    $.ajax({
        ...
        success: function (data) {
            if (!data.success) {
                alert('error');
            }
            dfd.resolve();
        }
        ...
    }); 

    return dfd.promise();
}

似乎大多数时间都是按照预期的顺序拨打电话,但并非总是如此。帮助我找到我确定我错过的一个小细节。

4

1 回答 1

1

看起来您在解决所有延迟DeleteItem之前调用您的函数。DeleteItemImage我认为你需要这样做。

var deferreds1 = [];  
var deferreds2 = [];   

...
// push()ing an unknown number of DeleteImage() ajax calls to deferred1 
// do not call DeleteItem here.  Wait until all of deferreds1 are resolved.
...

if (deferreds1.length > 0) {
    $.when
        .apply($, deferreds1)
        .done(function(){
             console.log('deferred1 done');
             // NOW call DeleteItem and add to deferreds2,
             // since now all the Images are deleted.
             deferreds2.push(DeleteItem()); // for each item...
             $.when
                  .apply($, deferreds2)
                  .done(function() {
                       console.log('deferred2 done');
                  })
                  .fail(function() {
                       console.log('deferred2 fail');
                  });
             })
         .fail(function(){
              console.log('deferred1 fail');
         });
}
于 2013-02-21T14:33:14.710 回答