bluebird

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

处理一个对象,对象的每一个属性都是异步的。
getPictures(),getComments(),getTweets()都是异步方法。

Promise.props({
 pictures: getPictures(),
 comments: getComments(),
 tweets: getTweets()
}).then(function(result) {
 console.log(result.tweets, result.pictures, result.comments);
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容