0

Here is my code:

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

I need to send a response after these two function setSentEmailCount and setReceivedEmailCount are completed for every contact. Both functions will update the objects inside the contactEmailArray. Objects are sentEmailCount and receivedEmailCount. Also, how can I minimize my code or use a single function for both the operations?

Mohit Maharjan
  • 79
  • 1
  • 3
  • 10
  • Yes, you should [promisify](https://stackoverflow.com/q/22519784/1048572) `gmailObject.estimatedMessages`. Have you tried it? – Bergi Oct 14 '17 at 19:47
  • @Bergi, if I wrap gmailObject.estimatedMessages to a promise, for every contact a new promise is created for both the functions. How will I know that all of my promises are resolved and send the response after that? – Mohit Maharjan Oct 15 '17 at 02:26
  • [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) will do that. – Bergi Oct 15 '17 at 10:05
  • do using promise make my both functions run in parallel? – Mohit Maharjan Oct 15 '17 at 16:10
  • No, starting two asynchronous things at once makes it parallel. Promises just make it simple to wait for one or more results and chain things sequentially. – Bergi Oct 15 '17 at 16:21
  • Ok, i will try to implement promises in my code. – Mohit Maharjan Oct 16 '17 at 08:30

1 Answers1

0

Maybe you can try to wrap those functions into promise and resolve it by es7 async/await ?

//async
await Promise.all([setSentEmailCount, setReceivedEmailCount])

Next idea for that, is to use middlewares, setSentEmailCount(gmail, currentContact, next) and after that use it - by next() in setReceivedEmailCount function ?

  • Those functions are inside a for loop. Will Promise.all work in this case? – Mohit Maharjan Oct 15 '17 at 02:30
  • Soo I will suggest to do something like: `contactEmailArray.map(contact => { [setSentEmailCount(contact, msg) , setReceivedEmailCount(contact, msg)] }) ` and in those functions returning a promise. In this case you will create array of promises and resolved it at the end. – Artur Figiel Oct 15 '17 at 10:44