【发布时间】:2017-01-13 17:20:13
【问题描述】:
我做了一个小函数,承诺返回 2 个数字(价格)。 我使用 for 循环得到了数字,但是当我运行代码时,它只将 1 个价格打印到控制台。
功能:
function getPrices(offer){
var price = [];
return new Promise(function(resolve, reject){
for(var i = 0; i < offer.length;i++){
market.getItemPrice(730, offer[i].market_hash_name, function(err, data) {
if(!err) {
try {
// JSON.parse() can throw an exception if not
resolve(data.lowest_price.match(/\$(\d+\.\d+)/)[1]);
} catch(e) {
reject(e);
}
//price += data.lowest_price.match(/\$(\d+\.\d+)/)[1];
}
});
}
});
}
获取值:
getPrices(offer.itemsToReceive).then(function(val){
var a = "";
a += val;
console.log(a);
}).catch(function(err) {
console.log(err);
});
它只打印出其中一个值: 0.05
【问题讨论】: