这是我的代码:
route.get('/',function(req, res){
for(var e = 0; e < contactEmailArray.length; e++){
var currentContact = contactEmailArray[e];
setSentEmailCount(gmail, currentContact);
setReceivedEmailCount(gmail, currentContact)
}
//send contactEmailArray as response after everything is completed
});
function setSentEmailCount(gmailObject, currentContact){
var s = gmailObject.estimatedMessages ('from:(mohitmaharjan@gmail.com) to:('+currentContact.email+')', { fields: []}, function(err, estimatedMessages){
currentContact.sentEmailCount = estimatedMessages;
console.log(currentContact.email + ' : ' + currentContact.sentEmailCount);
sentResponseCount++;
if(sentResponseCount == countOfContactHavingEmail){
console.log('sent operation completed');
}
console.log('Counter Value : ' + sentResponseCount);
});
}
function setReceivedEmailCount(gmailObject, currentContact){
var s = gmailObject.estimatedMessages ('from: ('+currentContact.email+') to:(mohitmaharjan@gmail.com)', { fields: []}, function(err, estimatedMessages){
currentContact.receivedEmailCount = estimatedMessages;
console.log(currentContact.email + ' : ' + currentContact.receivedEmailCount + ' received');
receivedResponseCount++;
if(sentResponseCount == countOfContactHavingEmail){
console.log(' received operation completed');
}
console.log('Counter Value : ' + receivedResponseCount);
});
}
我需要在每个联系人完成这两项功能 setSentEmailCount 和 setReceivedEmailCount 后发送回复。这两个函数都将更新contactEmailArray中的对象。对象是sentEmailCount和receivedEmailCount。另外,如何最小化我的代码或为这两个操作使用单个函数?
答案 0 :(得分:0)
也许您可以尝试将这些功能包装成承诺并通过es7 async / await解决它?
//async
await Promise.all([setSentEmailCount, setReceivedEmailCount])
接下来的想法是使用middlewares,setSentEmailCount(gmail, currentContact, next)
,之后使用next()
setReceivedEmailCount
函数?