What is retry-as-promised?
The retry-as-promised npm package allows you to retry a promise-returning function a specified number of times with a delay between each attempt. This is useful for handling transient errors in asynchronous operations, such as network requests or database queries.
What are retry-as-promised's main functionalities?
Basic Retry
This feature allows you to retry a promise-returning function up to a specified number of times (in this case, 3) with a delay between each attempt (in this case, 1000 milliseconds).
const retry = require('retry-as-promised');
const myFunction = () => {
return new Promise((resolve, reject) => {
// Simulate an operation that may fail
if (Math.random() > 0.5) {
resolve('Success');
} else {
reject('Failure');
}
});
};
retry(() => myFunction(), { max: 3, timeout: 1000 })
.then(result => console.log(result))
.catch(err => console.error(err));
Custom Retry Logic
This feature allows you to customize the retry logic, including the number of retries, the delay between retries, and the conditions under which to retry. In this example, the function will retry up to 5 times with an increasing delay based on the backoffBase and backoffExponent.
const retry = require('retry-as-promised');
const myFunction = () => {
return new Promise((resolve, reject) => {
// Simulate an operation that may fail
if (Math.random() > 0.5) {
resolve('Success');
} else {
reject('Failure');
}
});
};
retry(() => myFunction(), {
max: 5,
timeout: 2000,
match: [/Failure/],
backoffBase: 1000,
backoffExponent: 1.5
})
.then(result => console.log(result))
.catch(err => console.error(err));
Retry with Custom Error Handling
This feature allows you to specify custom error handling logic for retries. In this example, the function will retry up to 4 times with a delay that doubles each time, but only if the error message matches 'Custom Error'.
const retry = require('retry-as-promised');
const myFunction = () => {
return new Promise((resolve, reject) => {
// Simulate an operation that may fail
if (Math.random() > 0.5) {
resolve('Success');
} else {
reject(new Error('Custom Error'));
}
});
};
retry(() => myFunction(), {
max: 4,
timeout: 1500,
match: [err => err.message === 'Custom Error'],
backoffBase: 500,
backoffExponent: 2
})
.then(result => console.log(result))
.catch(err => console.error(err));
Other packages similar to retry-as-promised
promise-retry
The promise-retry package provides similar functionality for retrying promise-returning functions. It allows for customizable retry strategies, including exponential backoff and custom retry conditions. Compared to retry-as-promised, promise-retry offers more flexibility in defining retry strategies and conditions.
async-retry
The async-retry package is another alternative for retrying asynchronous operations. It supports customizable retry strategies, including exponential backoff and custom retry conditions. It is similar to retry-as-promised but offers additional features such as support for async/await syntax and more granular control over retry behavior.
p-retry
The p-retry package is a lightweight library for retrying promise-returning functions. It supports customizable retry strategies, including exponential backoff and custom retry conditions. Compared to retry-as-promised, p-retry is more focused on simplicity and ease of use, making it a good choice for straightforward retry scenarios.
retry-as-promised
Retry promises when they fail, based upon any-promise.
Installation
$ npm install --save retry-as-promised
$ yarn add retry-as-promised
Configuration
var retry = require('retry-as-promised');
var warningFn = function(msg){ someLoggingFunction(msg, 'notice'); };
return retry(function (options) {
return promise;
}, {
max: 3,
timeout: 10000
match: [
Sequelize.ConnectionError,
'SQLITE_BUSY'
],
backoffBase: 1000
backoffExponent: 1.5
report: warningFn,
name: 'SourceX'
});
Tested with