1. /**
  2. * Cleans up artifacts of file contents for validation checks with fixed retry intervals.
  3. *
  4. * @param {string} filePath The path to the file.
  5. * @param {function} validationFunction The function to validate the file contents. Should return true on success, false on failure.
  6. * @param {number} retryIntervalMs The interval in milliseconds between retry attempts.
  7. * @param {number} maxRetries The maximum number of retry attempts.
  8. * @returns {Promise<boolean>} A promise that resolves to true if the validation succeeds, or false if it fails after maxRetries attempts.
  9. */
  10. async function cleanupAndValidate(filePath, validationFunction, retryIntervalMs, maxRetries) {
  11. let retries = 0;
  12. while (retries < maxRetries) {
  13. try {
  14. const isValid = await validationFunction(filePath); // Validate file contents
  15. if (isValid) {
  16. return true; // Validation successful
  17. } else {
  18. retries++;
  19. await new Promise(resolve => setTimeout(resolve, retryIntervalMs)); // Wait before retrying
  20. }
  21. } catch (error) {
  22. retries++;
  23. await new Promise(resolve => setTimeout(resolve, retryIntervalMs)); // Wait before retrying
  24. console.error(`Validation failed with error: ${error}. Retry attempt: ${retries}`);
  25. }
  26. }
  27. return false; // Validation failed after maxRetries attempts
  28. }

Add your comment