Retry Mechanism
- retry:
retryis a versatile library that supports retrying both synchronous and asynchronous functions. It provides extensive configuration options, including customizable retry counts, delay strategies, and error handling. - p-retry:
p-retryfocuses on retrying promise-returning functions. It offers a clean API and allows you to specify the number of retries, delay between attempts, and a function to determine if a retry should occur. - promise-retry:
promise-retryallows you to retry a promise-returning function with customizable retry delays. You can specify the number of retries, the delay between attempts, and provide a function to calculate the delay dynamically. - async-retry:
async-retryprovides a simple way to retry asynchronous functions with customizable retry logic. It supports exponential backoff and allows you to define when to retry based on the error type. - retry-axios:
retry-axiosintegrates with Axios to provide automatic retry functionality for HTTP requests. It allows you to configure retry behavior, such as the number of retries and delay between attempts, directly on your Axios instance. - promise-poller:
promise-polleris designed for polling a promise-based function until it resolves or reaches a maximum number of attempts. It is useful for scenarios where you need to repeatedly check the status of an operation.
Customization
- retry:
retryoffers extensive customization options, including the ability to define your own retry strategy, set maximum retries, and customize delay functions. It is highly configurable and suitable for complex retry scenarios. - p-retry:
p-retryoffers customization for the number of retries, delay between attempts, and the ability to provide a custom retry strategy function. It is designed to be simple and intuitive while allowing for flexibility. - promise-retry:
promise-retryprovides customization for the number of retries, delay between retries, and the ability to use a custom delay function. It allows you to define how retries should be handled in a straightforward manner. - async-retry:
async-retryallows for significant customization, including the ability to define your own retry logic, set maximum retries, and customize the delay between retries. It also supports asynchronous delay functions. - retry-axios:
retry-axiosallows customization of the retry behavior for Axios requests, including the number of retries, delay between retries, and the ability to specify which HTTP status codes should trigger a retry. - promise-poller:
promise-pollerallows customization of the polling interval, maximum attempts, and the function used to check the promise. You can easily adjust these parameters to fit your use case.
Integration with Promises
- retry:
retrysupports both synchronous and asynchronous functions, providing flexibility in how you implement retries. It can handle any function type, including those that return promises. - p-retry:
p-retryis built specifically for promise-returning functions, ensuring a smooth integration with asynchronous code. It is lightweight and focuses solely on retrying promises. - promise-retry:
promise-retryis focused on retrying promise-returning functions, making it a great choice for async operations that may fail and need to be retried. - async-retry:
async-retryis designed for asynchronous functions and works seamlessly with promises. It can handle any function that returns a promise, making it versatile for various async operations. - retry-axios:
retry-axiosis specifically designed for Axios, a popular promise-based HTTP client. It adds retry functionality to Axios requests, making it easy to handle failed HTTP calls. - promise-poller:
promise-pollerintegrates well with promise-based functions, allowing you to poll a promise until it resolves. It is particularly useful for scenarios where you need to wait for a promise to complete.
Code Example
- retry:
Example of
retryconst retry = require('retry'); function unreliableTask() { // Simulate a task that may fail return new Promise((resolve, reject) => { const random = Math.random(); if (random < 0.7) { // 70% chance of failure reject(new Error('Task failed')); } else { resolve('Task succeeded'); } }); } const operation = retry.operation({ retries: 5, factor: 2, minTimeout: 1000, maxTimeout: 4000, }); operation.attempt(async (currentAttempt) => { try { const result = await unreliableTask(); console.log(result); } catch (error) { console.error(`Attempt ${currentAttempt} failed: ${error.message}`); if (operation.retry(error)) { console.log(`Retrying... (Attempt ${operation.attempts()})`); } } }); - p-retry:
Example of
p-retryconst { pRetry } = require('p-retry'); async function unreliableFunction() { // Simulate a function that fails throw new Error('Failed'); } async function main() { try { const result = await pRetry(unreliableFunction, { retries: 5, onFailedAttempt: (error) => { console.log(`Attempt failed: ${error.message}`); }, }); console.log('Success:', result); } catch (error) { console.error('All attempts failed:', error); } } main(); - promise-retry:
Example of
promise-retryconst promiseRetry = require('promise-retry'); function unreliableApiCall(retry, attempt) { // Simulate an API call that fails if (attempt < 3) { // Fail the first two attempts return retry(new Error('Temporary failure')); } // Succeed on the third attempt return 'Data from API'; } promiseRetry((retry) => unreliableApiCall(retry, attempt), { retries: 5, factor: 2, minTimeout: 1000, maxTimeout: 4000, }).then((result) => { console.log('API call succeeded:', result); }).catch((error) => { console.error('API call failed after retries:', error); }); - async-retry:
Example of
async-retryconst retry = require('async-retry'); async function fetchData() { // Simulate a function that may fail throw new Error('Network error'); } async function main() { try { const result = await retry(fetchData, { retries: 3, factor: 2, minTimeout: 1000, maxTimeout: 5000, }); console.log('Data:', result); } catch (error) { console.error('Failed after retries:', error); } } main(); - retry-axios:
Example of
retry-axiosconst axios = require('axios'); const { setup } = require('retry-axios'); const instance = axios.create(); setup(instance, { retries: 3, retryDelay: (retryCount) => { return retryCount * 1000; // Exponential backoff }, }); async function makeRequest() { try { const response = await instance.get('https://httpbin.org/status/500'); console.log('Response:', response.data); } catch (error) { console.error('Request failed:', error); } } makeRequest(); - promise-poller:
Example of
promise-pollerconst promisePoller = require('promise-poller'); async function pollerFunction() { // Simulate a polling function return new Promise((resolve, reject) => { const random = Math.random(); if (random < 0.8) { // 80% chance to fail reject(new Error('Not ready')); } else { // 20% chance to succeed resolve('Success!'); } }); } async function main() { try { const result = await promisePoller({ function: pollerFunction, interval: 1000, maxAttempts: 10, }); console.log('Polling result:', result); } catch (error) { console.error('Polling failed:', error); } } main();
