nodejs使用Q来解决未定义的promise nodejs

时间:2015-11-20 01:42:52

标签: node.js

作为一名php开发人员,我一直在用异步和同步功能包装,我从来不用担心这么多。所以我的问题是这个 我有一个功能

function loadPartnerInventory(args,itemIds,offers,offer) {
var deferred = Q.defer();

offers.loadPartnerInventory(args, function(err, items) {
    var checkItems = [];
    var len = items.length;
    for(var i = 0; i < itemIds.length; i++) {
        for(var j = 0; j < items.length; j++) {
            if (itemIds[i] == items[j].id){
                //console.log('Pushed: ' + items[j].name);

                checkItems.push({
                    itemname: items[j].market_name,
                    market_hash_name: items[j].market_hash_name,
                    steamid: offer.steamid_other,
                    tradeofferid : offer.tradeofferid
                });

            }
        }
    }
    deferred.resolve(checkItems);
});

return deferred.promise;
}

function loadMyInventory(args,itemIds_g,offers,offer) {
var deferred = Q.defer();
offers.loadMyInventory(args, function(err, itemsg) {

    var checkItems_g = [];
    var len = itemsg.length;
    for(var i = 0; i < itemIds_g.length; i++) {
        for(var j = 0; j < itemsg.length; j++) {
            if (itemIds_g[i] == itemsg[j].id){
                console.log('Pushed: ' + itemsg[j].name);

                checkItems_g.push({
                    itemname: itemsg[j].market_name,
                    market_hash_name: itemsg[j].market_hash_name,
                    steamid: offer.steamid_other,
                    tradeofferid : offer.tradeofferid
                });

            }
        }
    }
   deferred.resolve(checkItems_g);
});
return deferred.promise;
}

function getPartnerInventory(offers, offer, itemIds, itemIds_g) {
var p1 = loadPartnerInventory({
    partnerSteamId: offer.steamid_other,
    appId: 730,
    contextId: 2
},itemIds,offers,offer);

var p2 = loadMyInventory({appId: 730, contextId: 2},itemIds_g,offers,offer);

return Q.all(p1, p2).spread(function(checkItems, checkItems_g) {
    return {
        checkItems: checkItems,
        checkItems2: checkItems_g
    };
});
}

我这样做是为了得到结果,但不知怎的,第二个承诺是未定义的,我不明白为什么。

   getPartnerInventory(offers,offer,itemIds,itemIds_G).then(function(response) {
                               console.log(response);
                                //console.log(response);

                            });

checkitems正确返回但是checkitems 2未定义。

cosole log是:

{ checkItems:
{ itemname: 'Operation Breakout Weapon Case',
 market_hash_name: 'Operation Breakout Weapon Case',
 steamid: '76561198245431424',
 tradeofferid: '859881697' },
checkItems2: undefined }
Pushed: Glock-18 | Wraiths

可以看到它的未加工但似乎在完成之后添加了项目

1 个答案:

答案 0 :(得分:0)

你无法两次解决同一个承诺。您应该将这两个方法包装在单独的promise中,然后使用Q.all(),这将返回一个新的promise,只有在两个promise成功解析后才能解析。例如:

function mapOfferItem(item, offer) {
  return {
    itemname: item.market_name,
    market_hash_name: item.market_hash_name,
    steamid: args.offer.steamid_other,
    tradeofferid : args.offer.tradeofferid
  };
}

function loadPartnerInventory(args) {
  var deferred = Q.defer();

  offers.loadPartnerInventory(args, function(err, items) {
    var checkedItems = items.filter(function(item) => {
      return args.itemIds.indexOf(item.id) >= 0;
    }).map(function(item) {
      return mapOfferItem(item, args.offer);
    });
    deferred.resolve(checkedItems);
  });

  return deferred.promise;
}

function loadMyInventory(args) {
  var deferred = Q.defer();

  offers.loadMyInventory(args, function(err, items) {
    var checkItems = items.filter(function(item) {
      return args.itemIds_g.indexOf(item.id);
    }).map(function(item) {
      return mapOfferItem(item, args.offer);
    });
    deferred.resolve(checkItems);
  });

  return deferred.promise;
}

function getPartnerInventory(offers, offer, itemIds, itemIds_g) {
  var p1 = loadPartnerInventory({
    partnerSteamId: offer.steamid_other,
    appId: 730,
    contextId: 2
  });

  var p2 = loadMyInventory({appId: 730, contextId: 2});

  return Q.all([p1, p2]).spread(function(checkItems, checkItems_g) {
    return {
      checkItems: checkItems,
      checkItems2: checkItems_g
    };
  });
}

然后你可以使用这样的函数:

getParentInventory(offers, offer, itemIds, itemIds_g)
  .then(function(checkItems) {
    console.log(checkItems);
    // should print {checkItems: [...], checkItems2: [...]}
  });