retry-toolkit
v1.0.3
Published
The `retry-toolkit` is designed to address a common issue in software development: dealing with transient failures or temporary issues when performing operations such as network requests, API calls, or database queries. Here's a detailed explanation of th
Downloads
6
Readme
Retry Toolkit
The retry-toolkit
is designed to address a common issue in software development: dealing with transient failures or temporary issues when performing operations such as network requests, API calls, or database queries. Here's a detailed explanation of the problem it solves:
Handling Transient Failures: These are temporary issues that can occur intermittently and may be resolved with a subsequent retry. For example, network glitches, temporary server downtime, or rate limits imposed by APIs.
Manual Retry Logic: Developers often need to implement custom retry logic to handle these failures. This can involve writing repetitive and error-prone code to retry failed operations a specific number of times with a delay between attempts.
Configuration Complexity: Implementing retry logic requires configuring parameters like the number of retries, delay between attempts, and handling different types of errors. Doing this manually can be cumbersome and inconsistent across different parts of an application.
Installation
npm install retry-toolkit
Usage
const retry = require('retry-toolkit');
// Define a function that may fail
const fetchData = async () => {
console.log('Attempting to fetch data...');
throw new Error('Simulated fetch failure');
};
// Use retry-package with exponential backoff
retry(fetchData, {
retries: 5, // Retry up to 5 times
delay: 1000, // Initial delay of 1 second
backoff: 'exponential', // Exponential backoff
retryOnError: (error) => error.message !== 'Non-retryable error', // Retry only on certain errors
timeout: 10000, // Timeout for each attempt set to 10 seconds
onRetry: (attemptNumber, error) => {
console.log(`Retry attempt ${attemptNumber} failed: ${error.message}`);
},
onFailure: (error) => {
console.error('Final failure after retries:', error.message);
}
}).then(data => {
console.log('Data fetched successfully:', data);
}).catch(error => {
console.error('Error:', error.message);
});