I have a for loop
for (let i = 0; i < options.length; i++) {
console.log("Entered the to for " + i);
let employee = await this.prolesChecker.getEmployeesFromEmail(options[i]);
let isOnVacation = await this.prolesChecker.isOnVacation(employee, moment());
}
The two functions "getEmployeesFromEmail and isOnVacation" are connecting to a database and they need some time until result is returned. I want the for loop to wait until the result is returned and then go in the next iteration.
As an example the console.log always prints
Entered the to for 0
It never goes to i = 1
Here is the function
public async deleteEmailsTo(options: any) {
console.log(options.length);
for (let i = 0; i < options.length; i++) {
console.log("Entered the to for " + i);
let employee = await this.prolesChecker.getEmployeesFromEmail(options[i]);
let isOnVacation = await this.prolesChecker.isOnVacation(employee, moment());
if ((!employee.EmailReminders && !isOnVacation) || (!employee.EmailReminders && !employee.EmailRemindersForHoliday && isOnVacation)) {
let index = options.indexOf(options[i], 0);
if (index > -1) {
options.splice(index, 1);
console.log("Removed " + employee.Name + " " + employee.LastName + " from the 'to' list");
}
}
}
}
Any suggestions please?
asyncgetEmployeesFromEmailandisOnVacationconsole.logasync/awaitsyntax are far superior to the async.js module.