jQuery Deferred's, $.when() and the fail() callback arguments

后端 未结 4 499
灰色年华
灰色年华 2020-12-01 16:08

I\'m getting an unexpected result when using $.when() when one of the deferred operations does not succeed.

Take this JavaScript, which created 2 deferr

相关标签:
4条回答
  • 2020-12-01 16:25

    http://jsfiddle.net/InfinitiesLoop/yQsYK/

    This will always reject if given multiple inputs. rejected = true; should be rejected |= reject;

    0 讨论(0)
  • 2020-12-01 16:28

    $.when() will execute the failed callback (2nd parameter passed to then()) immediately if any one of the parameters fails. It's by design. To quote the documentation:

    http://api.jquery.com/jQuery.when/

    In the multiple-Deferreds case where one of the Deferreds is rejected, jQuery.when immediately fires the failCallbacks for its master Deferred. Note that some of the Deferreds may still be unresolved at that point. If you need to perform additional processing for this case, such as canceling any unfinished ajax requests, you can keep references to the underlying jqXHR objects in a closure and inspect/cancel them in the failCallback.

    There's actually no built-in way of getting a callback that waits untils all of them are finished regardless of their success/failure status.

    So, I built a $.whenAll() for you :)
    It always waits until all of them resolve, one way or the other:

    http://jsfiddle.net/InfinitiesLoop/yQsYK/51/

    $.whenAll(a, b, c)
        .then( callbackUponAllResolvedOrRejected );
    
    0 讨论(0)
  • 2020-12-01 16:39

    Internally, the "reject" and "fail" paths are handled by two totally separate queues, so it just doesn't work the way you expect.

    In order to know which original Deferred failed from the "when()" group, you could have them pass themselves along with the ".reject()" call as part of an object literal or something.

    0 讨论(0)
  • 2020-12-01 16:49

    I've faced this same problem, and I dealt with it by using the .always callback and inspecting my array of deferred objects. I had an unknown number of ajax calls, so I had to do the following:

    // array of ajax deletes
    var deletes = [];
    $checkboxes.each(function () {
        deletes.push(deleteFile(this));
    });
    
    $.when.apply($, deletes)
      .always(function () {
          // unfortunately .fail shortcircuits and returns the first fail,
          // so we have to loop the deferred objects and see what happened.
    
          $.each(deletes, function () {
              this.done(function () {
                  console.log("done");
              }).fail(function () {
                  console.log("fail");
              });
          });
      });
    

    The deleteFile method returns a promise, which has .done or .fail callbacks.

    This allows you to take action after all deferreds have completed. In my case I'm going to show a delete file error summary.

    I just tried this, and unfortunately I had to put a interval timer to check that they were all truly done after my $.each on the deferred objects. This seems odd and counterintuitive.

    Still trying to understand these deferreds!

    0 讨论(0)
提交回复
热议问题