如何在循环中记录每个承诺的执行结果,以便在执行完成后进行评估?我需要根据承诺结果重定向到错误页面或成功页面。步骤如下:
1.调用检索详细信息
2.如果解决了上述问题,请致电updateDetails
3.以上两个步骤针对每个关联循环执行
4.确定所有承诺的完成情况并评估结果
5.根据结果定义导航
到目前为止,我收到错误消息“发送标头后无法设置。”
这是伪代码
let currentData;
let promises = [];
let updateResponse = 'SUCCESS';
let failureFlag;
let failureMsg;
let retrieveData = function(networkID, number) {
return new Promise(function(resolve, reject) {
retrieveService.retrieveDetails(networkID, number, function(err, result) {
if (err) {
failureFlag = true;
reject(err);
}
currentData = result;
resolve();
});
})
.catch((error) => {
failureMsg = getfailureMsg();
failureFlag = true;
});
};
data.associations.forEach(function(association) {
var getIndicator = retrieveData(loggedOnUser, association.location);
getIndicator
.then(function() {
let updateIndicator = function(loggedOnUser, key, number, currentData) {
return new Promise(function(resolve, reject) {
updateService.updateDetails(loggedOnUser, key, number, currentData, function(err, result) {
if (err) {
failureFlag = true;
reject(err);
} else if (result) {
if (result == updateResponse) {
resolve();
} else {
failureFlag = true;
reject(result);
}
}
});
})
.catch((error) => {
failureMsg = getfailureMsg();
failureFlag = true;
});
};
if (currentData) {
var addIndicator = updateIndicator(loggedOnUser, key, association.location, currentData);
}
addIndicator
.then(function() {
promises.push(getIndicator);
})
})
});
Promise.all(promises)
.then(function() {
if (failureFlag) {
req.flash('info', 'failure');
res.redirect('/staffSummary');
} else {
req.flash('info', 'success');
res.redirect('/staffSummary');
}
})
}