Created
          August 8, 2025 17:49 
        
      - 
      
- 
        Save VehpuS/251bd0e752009b3498094925fa12b666 to your computer and use it in GitHub Desktop. 
    Perform an asynchronous operation with expnantially backed off + jitter retries
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const doAsyncOperationWithRetry = async <T>( | |
| operation: () => Promise<T>, | |
| { | |
| retry = true, | |
| maxRetries = 3, | |
| }: { | |
| retry?: boolean; | |
| maxRetries?: number; | |
| } = {} | |
| ): Promise<T> => { | |
| let attempt = 0; | |
| let lastAttemptError: any; | |
| while (attempt < (retry ? maxRetries : 1)) { | |
| try { | |
| // Exponential backoff delay on retries with jitter | |
| if (attempt > 0) { | |
| const baseDelay = Math.pow(2, attempt) * 1000; // 2s, 4s, 8s | |
| const jitter = Math.floor(Math.random() * 1000); // up to 1s random jitter | |
| const delay = baseDelay + jitter; | |
| await new Promise((resolve) => setTimeout(resolve, delay)); | |
| console.log( | |
| `Retry attempt ${attempt} after ${delay}ms delay (base: ${baseDelay}ms, jitter: ${jitter}ms)` | |
| ); | |
| } | |
| attempt++; | |
| // Execute the operation | |
| return await operation(); | |
| } catch (error: any) { | |
| lastAttemptError = error; | |
| console.error( | |
| `Error occurred during async operation (attempt ${attempt}/${maxRetries}):`, | |
| error | |
| ); | |
| // If we've exhausted retries, throw the error | |
| if (attempt >= maxRetries) { | |
| throw error; | |
| } | |
| // If it's not a retryable error, don't retry | |
| if (!isRetryableError(error)) { | |
| throw error; | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment