我有一个大型数组,我想循环并执行promises但我的循环在promises结束之前结束。他们是一种我可以通过循环承诺并且能够在承诺循环结束后执行某些事情的方式。这是我的代码。
Meters.getMetersByResidentId(resident.id)
.then(function(meters){
_.each(meters, function(meter){
var meter_obj = {};
var meter_obj_arr = [];
Servers.getServerIdByMeterId(meter.id)
.then(function(server_id){
Meters.getNameByMeterId(meter.id)
.then(function(name){
Dials[server_id[0].server_id].getPreviousTotalByChannelServerAndSN(server_id[0].server_id,name[0].meter_id,billing_id)
.then(function(previous_total){
Dials[server_id[0].server_id].getCurrentTotalByChannelServerAndSN(server_id[0].server_id,name[0].meter_id,billing_id)
.then(function(current_total){
Promise.all([Billings.getStartDateById(billing_id),Billings.getEndDateById(billing_id)])
.then(function(result){
meter_obj.meter_no = meter.meter_id;
if (meter.type == 1) meter_obj.meter_type = "EL";
else if (meter.type == 0) meter_obj.meter_type = "W";
meter_obj.days = Math.round(Math.abs((result[0][0].start_date.getTime() - result[1][0].end_date.getTime())/(24*60*60*1000)));
meter_obj.prev_read_date = dateFormat(result[0][0].start_date, "mediumDate");
meter_obj.current_read_date = dateFormat(result[1][0].end_date,"mediumDate");
meter_obj.code = "MR";
meter_obj.current_reading = current_total[0].total;
meter_obj.prev_reading = previous_total[0].total;
meter_obj.multiplier = meter.multiplier;
meter_obj.usage = Math.round((meter_obj.current_reading - meter_obj.prev_reading)*meter_obj.multiplier*100)/100;
if (meter.type == 1) meter_obj.units = "KWh";
else if (meter.type ==0) meter_obj.units = "m3"
meter_obj_arr.push(meter_obj);
invoice_obj.meters = meter_obj_arr;
});
});
});
});
});
});
});
我希望在数组中按下所有仪表数据后显示invoice_obj
。
答案 0 :(得分:1)
Meters.getMetersByResidentId(resident.id)
.then(function (meters) {
var metersLoop = []
_.each(meters, function (meter) {
metersLoop.push(function () {
return Servers.getServerIdByMeterId(meter.id)
.then(function (server_id) {
return Meters.getNameByMeterId(meter.id)
.then(function (name) {
return Dials[server_id[0].server_id].getPreviousTotalByChannelServerAndSN(server_id[0].server_id, name[0].meter_id, billing_id)
.then(function (previous_total) {
return Dials[server_id[0].server_id].getCurrentTotalByChannelServerAndSN(server_id[0].server_id, name[0].meter_id, billing_id)
.then(function (current_total) {
return Promise.all([Billings.getStartDateById(billing_id), Billings.getEndDateById(billing_id)])
.then(function (result) {
var meter_obj = {
meter_no: meter.meter_id,
days: Math.round(Math.abs((result[0][0].start_date.getTime() - result[1][0].end_date.getTime()) / (24 * 60 * 60 * 1000))),
prev_read_date: dateFormat(result[0][0].start_date, "mediumDate"),
current_read_date: dateFormat(result[1][0].end_date, "mediumDate"),
code: "MR",
current_reading: current_total[0].total,
prev_reading: previous_total[0].total,
multiplier: meter.multiplier,
usage: Math.round((meter_obj.current_reading - meter_obj.prev_reading) * meter_obj.multiplier * 100) / 100
}
if (meter.type == 1) meter_obj.meter_type = "EL";
else if (meter.type == 0) meter_obj.meter_type = "W";
if (meter.type == 1) meter_obj.units = "KWh";
else if (meter.type == 0) meter_obj.units = "m3"
return meter_obj
})
})
})
})
})
})
})
return Promise.all(metersLoop)
}).then(function (meter_obj_arr) {
console.log(meter_obj_arr)
})
})