.When() and .done() using an array with .done

前端 未结 3 458
一整个雨季
一整个雨季 2020-12-07 05:08

I am writing a google content script and my program needs to make roughly 30 AJAX calls to the server. I am using JQuery\'s .when function in conjunction with .apply to pas

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 05:52

    If you look at the examples for $.when, you see that the call back gets passed an argument for each promise. If that promise came from an Ajax call, then each argument is an array of the form [ data, statusText, jqXHR ].

    So you you just have iterate over the arguments and extract the first element. $.map makes that very easy:

    $.when.apply($, requests)
      .then(function() {
        return $.map(arguments, function(v) {
          return v[0];
        });
      })
      .done(callback);
    

提交回复
热议问题