I have such function:
async howLongToMyBirthdayDate(date) {
return await new Promise((resolve, reject) => {
let bday;
if (!(date instanceof Date)) {
if (Number.isInteger(date) && date > 0) {
bday = new Date(date);
} else {
setTimeout(() => reject(new Error('Wrong argument!')), 1000);
return;
}
} else {
bday = date;
}
const today = new Date();
today.setHours(0, 0, 0, 0);
bday.setFullYear(today.getFullYear());
bday.setHours(0, 0, 0, 0);
if (bday - today === 0) {
setTimeout(() => resolve(this.congratulateWithBirthday()), 1000);
}
setTimeout(
() =>
resolve(this.notifyWaitingTime(Math.ceil((bday - today) / 86400000))),
1000
);
});
}
The promise resolves twice. I see the result of function congratulateWithBirthday and notifyWaitingTime. Is it normal? I thought that promise can be resolved or rejected only once - by the first invokation of resolve or reject callback. Maybe setTimeout changes the behaviour of it? Can anyone explain me, please?