Retry Logic
retryUntilSuccess()
Executes an async action with automatic retry logic until it succeeds or exhausts all attempts.| Parameter | Type | Default | Description |
|---|---|---|---|
action | () => Promise<T> | (required) | Async function to execute |
retries | number | 5 | Maximum number of retry attempts |
delayMs | number | 1000 | Delay between retries in milliseconds |
label | string | "acción" | Descriptive label for logging |
Promise<T>
- Returns the result of
action()on success - Throws the last error after all retries are exhausted
How It Works
Handle Failure
If the action throws an error:
- Logs a warning with attempt number
- Waits for the specified delay
- Retries the action
Usage Examples
Basic Usage
With Custom Configuration
Wrapping Playwright Actions
Logging Output
The function provides clear console feedback: During Retries:Real-World Usage in Codebase
This utility is extensively used throughoutfunctions.ts to make browser automation robust:
- Login
- Product Selection
- Warehouse Selection
- Prepare Row
- Fill Data
- Payment Selection
Why Retry Logic is Critical
Web automation faces numerous transient failures:- Network delays: API responses or page loads
- DOM timing: Elements not yet rendered
- Angular/React updates: State changes in progress
- Animation interference: Elements moving during clicks
- Race conditions: Multiple async operations
- Server-side processing: Backend delays
retryUntilSuccess() utility makes the scraper resilient to all these issues without cluttering code with manual retry logic.
Best Practices
Choose Appropriate Retry Counts
Choose Appropriate Retry Counts
- Fast operations (clicks, fills): 5 retries (default)
- Slow operations (page loads): 10+ retries
- Critical operations (login): 10+ retries with longer delays
Set Meaningful Labels
Set Meaningful Labels
Good labels help debug failures:
Adjust Delays for Operation Type
Adjust Delays for Operation Type
- UI interactions: 1000ms (default)
- API calls: 2000-5000ms
- File operations: 500-1000ms
Keep Actions Idempotent
Keep Actions Idempotent
Ensure retried actions can be safely repeated:
String Utilities
agregarEspacios()
Adds leading and trailing spaces to a string.str: Input string
" " + str + " "
Why This Exists
Siigo’s autocomplete fields often require exact matches including spacing. Many account names and warehouse names in Siigo have leading/trailing spaces:" BODEGA DE RIOHACHA "" CAJA RIOHACHA "" Efectivo "
Usage Examples
Current Usage
While imported intransformDs.ts, this utility is currently used for hardcoded values in main.ts:
Error Handling Pattern
Combining retry logic with proper error handling:Performance Considerations
Retry Budget
With default settings (5 retries, 1000ms delay), each operation has:- Maximum time: ~5 seconds (if all retries fail)
- Typical time: 0-2 seconds (succeeds on first or second attempt)
Optimization Tips
- Start with lower retries for operations that typically succeed
- Increase retries only for known-flaky operations
- Use exponential backoff for rate-limited APIs:
Testing Helpers
Utilities for testing retry logic:Dependencies
Related Documentation
- Core Functions - All browser automation functions use
retryUntilSuccess() - Data Transformations - Imports
agregarEspacios()for string formatting - Troubleshooting - Comprehensive debugging strategies
- Configuration - Configure environment variables