第二个承诺不能正常运作

时间:2017-06-04 17:23:20

标签: javascript ajax promise

我有以下promise完美运作:

self.getAll = function (callback) {
    var users= [];
    var promises = [];
    $.ajax({
        url: "/API/Users",
        type: "GET",
        success: function (results) {
            var mappedContacts = $.map(results, function (item) {
                promises.push($.ajax({
                    url: "/API/Users/contacts/" + item.id,
                    type: "GET"
                }).then(function (contacts) {
                    users.push(new User(item, contacts));
                }));
            });
            $.when.apply($, promises).then(function () {
                callback(users);
            });
        }
    });
}

我试图添加第二个AJAX请求,但它无法正常运行:

self.getAll = function (callback) {
    var users= [];
    var promises = [];
    $.ajax({
        url: "/API/Users",
        type: "GET",
        success: function (results) {
            var mappedContacts = $.map(results, function (item) {
                promises.push($.ajax({
                    url: "/API/Users/contacts/" + item.id,
                    type: "GET"
                }).then(function (contacts) {
                    users.push(new User(item, contacts));
                }));
            });
            var mappedContacts2 = $.map(results, function (item) {
                promises.push($.ajax({
                    url: "/API/Users/contacts2/" + item.id,
                    type: "GET"
                }).then(function (contacts2) {
                    users.push(new User(item, "",contacts2));
                }));
            });
            $.when.apply($, promises).then(function () {
                callback(users);
            });
        }
    });
}

contacts2总是空的,我做错了什么?

这是User对象:

var User= function (data, contacts, contacts2) {
    this.id = ko.observable(data.id);
    this.name = ko.observable(data.name);
    this.contacts = ko.observableArray(contacts);
    this.contacts2 = ko.observableArray(contacts2 );
}

1 个答案:

答案 0 :(得分:1)

由于您需要将每个用户的两组联系人传递给new User(),请使用一个map()为两个联系人请求返回$.when()。在then()

$.when()中创建用户

类似的东西:

self.getAll = function(callback) {
  var users = [];
  // return this promise ..... see notes below
  return $.getJSON("/API/Users").then(results) {

    // map array of promises to pass to final $.when
    var promises = $.map(results, function(item) {

      var req1 = $.getJSON("/API/Users/contacts/" + item.id);
      var req2 = $.getJSON("/API/Users/contacts2/" + item.id);

      // return this promise to mapped array
      return $.when(req1, req2).then(function(contacts1, contacts2) {
        // create user now that we have both sets of contacts
        users.push(new User(item, contacts1, contacts2));
      });
    })

    // should return this promise .... see notes below
    return $.when.apply($, promises).then(function() {
      callback(users);
      // return `users` ...see notes below
    });

  })
}

当您可以返回上面评论中显示的承诺链时,使用回调是一种过时的方法并执行:

self.getAll().then(function(users) {
  // do something with users
})