1. /**
  2. * Checks if a date value falls within specified constraints with a timeout.
  3. *
  4. * @param {Date} dateValue The date value to check.
  5. * @param {Date} minDate The minimum acceptable date.
  6. * @param {Date} maxDate The maximum acceptable date.
  7. * @param {number} timeoutMs The timeout in milliseconds.
  8. * @returns {Promise<boolean>} A promise that resolves to true if the date is within
  9. * the constraints, false otherwise, or rejects if the timeout is exceeded.
  10. */
  11. async function checkDateConstraints(dateValue, minDate, maxDate, timeoutMs) {
  12. return new Promise((resolve, reject) => {
  13. const startTime = Date.now();
  14. if (!(dateValue instanceof Date) || !(minDate instanceof Date) || !(maxDate instanceof Date)) {
  15. reject(new Error("Invalid input: All arguments must be Date objects."));
  16. return;
  17. }
  18. if (dateValue < minDate || dateValue > maxDate) {
  19. resolve(false); // Date is outside the range
  20. return;
  21. }
  22. if (Date.now() - startTime > timeoutMs) {
  23. reject(new Error("Timeout exceeded."));
  24. return;
  25. }
  26. resolve(true); // Date is within the range
  27. });
  28. }

Add your comment