1.Promise.reduce
遍歷異步方法:
Promise.reduce(historyResults,function (arr,historyResult) {
return ApiUsers.find({_id:historyResult._id.user_id},{name:1,qxb_balance:1})
.then(function (apiResults) {
var estimatedAmount = (historyResult.amount/dateDiff) * 7;
if(apiResults[0].qxb_balance<estimatedAmount){
arr.push({
user_id : apiResults[0]._id.toString(),
qxb_balance:apiResults[0].qxb_balance,
name:apiResults[0].name,
cost:historyResult.amount
})
return arr;
}
})
},[]).then(function (data) {
// console.log(data);
return resolve(data);
})
2.Promise.props
處理一個(gè)對(duì)象,對(duì)象的每一個(gè)屬性都是異步的录语。
getPictures()倍啥,getComments(),getTweets()都是異步方法澎埠。
Promise.props({
pictures: getPictures(),
comments: getComments(),
tweets: getTweets()
}).then(function(result) {
console.log(result.tweets, result.pictures, result.comments);
});