将2个ajax调用合并为1个回调

时间:2015-10-05 19:06:44

标签: javascript jquery ajax google-chrome-extension callback

我正在构建Chrome扩展程序,我需要组合2个单独的AJAX调用,以便成功进行1次回调。最好的方法是什么?

Auth.prototype.updateContact = function(id, contact_obj) {
  var self = this,
      contact_str = JSON.stringify(contact_obj);

  return new RSVP.Promise(function(resolve, reject) {
    self.authorize()
      .then(function() {
        $.ajax({
          type: "PUT",
          url: self.url + "contacts/" + id,
          contentType: "application/json; charset=utf-8",
          data: contact_str,
          dataType: "json",
          success: function(data) {
            resolve(data);
          },
          error: function(jqXHR, textStatus, errorThrown) {
            var msg = "updateContact error: request: " + id + " " +
                  contact_str + " response: " + jqXHR.responseText +
                  " e=" + JSON.stringify(errorThrown);
            sendErrorBackground(msg);
            reject(jqXHR);
          }
        });
      });
  });
};

Auth.prototype.updateContactList = function(id, list_obj) {
  var self = this,
      list_str = JSON.stringify(list_obj);

  return new RSVP.Promise(function(resolve, reject) {
    self.authorize()
      .then(function() {
        $.ajax({
          type: "POST",
          url: self.url + "add_lists",
          contentType: "application/json; charset=utf-8",
          data: list_str,
          dataType: "json",
          success: function(data) {
            resolve(data);
          },
          error: function(jqXHR, textStatus, errorThrown) {
            var msg = "updateContactList error: request: " + id + " " +
                  list_str + " response: " + jqXHR.responseText +
                  " e=" + JSON.stringify(errorThrown);
            sendErrorBackground(msg);
            reject(jqXHR);
          }
        });
      });
  });
};

尝试使用@ Saar的建议

Auth.prototype.updateContact = function(id, contact_obj, list_obj) {
  var self = this,
      contact_str = JSON.stringify(contact_obj),
      list_str = JSON.stringify(list_obj);


  var promiseA = new RSVP.Promise(function(resolve, reject) {
        self.authorize().then(function() {
          $.ajax({
            type: "PUT",
            url: self.url + "contacts/" + id,
            contentType: "application/json; charset=utf-8",
            data: contact_str,
            dataType: "json",
            success: function(data) {
              return data
            }
          });
        });
      });

  var promiseB = new RSVP.Promise(function(resolve, reject) {
    self.authorize().then(function() {
        $.ajax({
          type: "POST",
          url: self.url + "add_lists",
          contentType: "application/json; charset=utf-8",
          data: list_str,
          dataType: "json",
          success: function(data) {
            return data
          }
        });
      });
    });

  $.when(promiseA, promiseB).then(function(resultA, resultB) {
    console.log(resultB);
  });
};

1 个答案:

答案 0 :(得分:1)

如果您使用RSVP承诺,那么您需要使用“全部”

Auth.prototype.updateContact = function (id, contact_obj, list_obj) {
    var self = this,
        contact_str = JSON.stringify(contact_obj),
        list_str = JSON.stringify(list_obj);


    var promiseA = new RSVP.Promise(function (resolve, reject) {
        self.authorize().then(function () {
            $.ajax({
                type: "PUT",
                url: self.url + "contacts/" + id,
                contentType: "application/json; charset=utf-8",
                data: contact_str,
                dataType: "json",
                success: function (data) {
                    return data
                }
            });
        });
    });

    var promiseB = new RSVP.Promise(function (resolve, reject) {
        self.authorize().then(function () {
            $.ajax({
                type: "POST",
                url: self.url + "add_lists",
                contentType: "application/json; charset=utf-8",
                data: list_str,
                dataType: "json",
                success: function (data) {
                    return data
                }
            });
        });
    });


    var promises = [promiseA, promiseB];

    RSVP.all(promises).then(function (results) {
        // results contains an array of results for the given promises
        console.log(results);
    }).catch(function (reason) {
        // if any of the promises fails.
    });
};