我需要在承诺之外得到结果。这是我的代码。
var NotList = share.getNotification();
Promise.all([NotList]).then(function(not_list) {
var report_list = not_list[0];
for (var n = 0; n < report_list.length; n++) {
var time_d = report_list[n].time;
var not_id = report_list[n]._id;
var PlantData = plant_details(report_list[n].plant_id);
Promise.all([PlantData, not_id]).then(function(result_arr) {
var plant_d = result_arr[0];
return notification_arr.push({notification_id: result_arr[1], parameter_name: "op_conductivity_val", plant_title: plant_d.title, message: "Plant output conductivity is low", notification_class: "danger", time: time_d});
});
}
});
console.log(notification_arr);
但是我得到一个空数组。请帮助某人解决这个问题。
答案 0 :(得分:2)
var NotList = share.getNotification();
var notification_arrPromise = new Promise(resolve => {
Promise.all([NotList]).then(function(not_list) {
var report_list = not_list[0];
var count = 0;
for (var n = 0; n < report_list.length; n++) {
var time_d = report_list[n].time;
var not_id = report_list[n]._id;
var PlantData = plant_details(report_list[n].plant_id);
Promise.all([PlantData, not_id]).then(function(result_arr) {
var plant_d = result_arr[0];
count++;
if(count === report_list.length){
resolve (notification_arr.push({notification_id: result_arr[1], parameter_name: "op_conductivity_val", plant_title: plant_d.title, message: "Plant output conductivity is low", notification_class: "danger", time: time_d}));
}
});
}
});
});
notification_arrPromise.then(function(notification_arr){
console.log(notification_arr);
});