I found a lot of question here related to it, but I can't figure out how to solve my specific problem.
I have a function that gets email users and send them an email. After each email sent, I add the respective user id in an array. Now I'm implementing the logger, so I need to save in the log the quantity of emails that were sent, to do that I just need to get the array length.
The problem is that is async, how can I get the array length only after sending the emails to all users?
async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
        await callback(array[index], index, array);
    }
}
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
var sentMail = [];
module.exports = {
    sendReminderMail: function(db, mail, consts){                
            db.find({ $and: [{ subscribed: true }] }, { $not: { feedback: true } }] }, async (err, result) => {        
                if (err) {                    
                    logger.error(`${err.status || 500} - ${err} - '/sendReminderMail' - 'Schedule'`);
                } else {
                    await asyncForEach(result, async (subscriber, index) => {
                        await waitFor(2000 * index);                         
                        if (sentMail.indexOf(subscriber._id) < 0) {
                            mail.sendMail(subscriber, consts.emailType.REMINDER);                       
                            sentMail.push(subscriber._id);                                
                        }
                    });            
                }
            });
     }
}
I tried that, but in this case, the logger is called after each interation:
const waitFor = (ms) => new Promise(r => setTimeout(r, ms)).then(
logger.info(sentMail.length + 'mails was sent'));
Suppose that my sentMail array is this [123, 987,2545], so my logger should save 3 mails were sent.
 
     
    