I am calling a method sendEmail that sends an email to a single user or a bunch of users(the type of to email is string | string[])
Is there a cleaner/better way to determine if it's an array or a single user so that I can look for it in the DB accordingly? The code looks quite repetitive now.
public async sendEmail(
toEmail: string | string[],
): Promise<void> {
if (!Array.isArray(toEmail)) {
const user = await this.userRepository.getUserByEmail(toEmail);
await checkIfPaymentMade(user);
} else {
for (const email of toEmail) {
const user = await this.userRepository.getUserByEmail(toEmail);
await checkIfPaymentMade(user);
}
}
}