router.get('/test.node', function(req, res, next) {
//定义回显的msg
var msg = {
stats: 1,
err: [],
main: {},
mark: ''
};
//查询数据1
var paywayList = [];
//查询数据2
var tradeList = [];
//查询数据3
var rechargeList = [];
//查询数据4
var tradeSum = {};
//每查询一次调用一次okCountFunc()
//查询数据1
bossService.getPaywayListByMerchant(merchantId, branchId, function(items) {
paywayList = items;
okCountFunc();
});
//查询数据2
bossService.getPaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(items) {
tradeList = items;
okCountFunc();
});
//查询数据3
bossService.getVippaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(items, objs) {
rechargeList = objs;
okCountFunc();
});
//查询数据4
bossService.getDaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(list, sum) {
tradeSum = sum;
okCountFunc();
});
//统计计数
var okCount = 0;
var okCountFunc = function() {
okCount++;
if (okCount == 4) { //到这里查询已全部结束
//最后合并该统计数据
msg.main.paywayList = paywayList;
msg.main.tradeList = tradeList;
msg.main.rechargeList = rechargeList;
msg.main.tradeSum = tradeSum;
res.send(msg);
}
}
});
Is there a more convenient way to echo back to the page after all queries are executed?
promise? Is there a demo.
phpcn_u15822017-05-02 09:28:15
var msg = {
stats: 1,
err: [],
main: {},
mark: ''
};
const task1 = new Promise((resolve, reject) => {
bossService.getPaywayListByMerchant(merchantId, branchId, (items) => {
msg.main.paywayList = items;
return resolve();
});
});
const task2 = ...
Promise.all([task1, task2, ...]).then(() => {
res.send(msg);
});