/**
* Checks if a date value falls within specified constraints with a timeout.
*
* @param {Date} dateValue The date value to check.
* @param {Date} minDate The minimum acceptable date.
* @param {Date} maxDate The maximum acceptable date.
* @param {number} timeoutMs The timeout in milliseconds.
* @returns {Promise<boolean>} A promise that resolves to true if the date is within
* the constraints, false otherwise, or rejects if the timeout is exceeded.
*/
async function checkDateConstraints(dateValue, minDate, maxDate, timeoutMs) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
if (!(dateValue instanceof Date) || !(minDate instanceof Date) || !(maxDate instanceof Date)) {
reject(new Error("Invalid input: All arguments must be Date objects."));
return;
}
if (dateValue < minDate || dateValue > maxDate) {
resolve(false); // Date is outside the range
return;
}
if (Date.now() - startTime > timeoutMs) {
reject(new Error("Timeout exceeded."));
return;
}
resolve(true); // Date is within the range
});
}
Add your comment