Guest User

Untitled

a guest
Jul 4th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. async function retryOperation(operation, maxRetries = 3, delay = 5000) {
  2. for (let attempt = 1; attempt <= maxRetries; attempt++) {
  3. try {
  4. return await operation();
  5. } catch (error) {
  6. console.log(`Attempt ${attempt} failed: ${error.message}`);
  7.  
  8. if (attempt === maxRetries) {
  9. throw new Error(`Operation failed after ${maxRetries} attempts: ${error.message}`);
  10. }
  11.  
  12. console.log(`Retrying in ${delay / 1000} seconds...`);
  13. await new Promise(resolve => setTimeout(resolve, delay));
  14. }
  15. }
  16. }
  17.  
  18. // Usage example for waitForSelector
  19. try {
  20. const input = await retryOperation(
  21. () => page.waitForSelector('#kodWydzialuInput', { timeout: 30000 }),
  22. 3, // maxRetries
  23. 5000 // delay
  24. );
  25. // Continue with your code using the input element
  26. } catch (error) {
  27. console.error('Error:', error.message);
  28. // Handle the error
  29. }
Advertisement
Add Comment
Please, Sign In to add comment