gpt4 book ai didi

promise - 使用 sequelize 的 promise 循环

转载 作者:行者123 更新时间:2023-12-03 22:28:11 25 4
gpt4 key购买 nike

我有一个大数组,我想遍历它并对其执行 promise ,但我的循环在 promise 结束之前结束。他们是我可以通过循环 promise 并能够在 promise 循环结束后执行某些事情的一种方式。这是我的代码。

  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

最佳答案

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)
})
})

关于promise - 使用 sequelize 的 promise 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44849209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com