Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
promise-retry
Advanced tools
Retries a function that returns a promise, leveraging the power of the retry module.
The promise-retry npm package allows users to retry a promise-returning or an async function until it resolves or reaches a maximum number of attempts. It is useful for handling operations that may temporarily fail due to external factors such as network issues or service unavailability.
Basic retry functionality
This feature allows you to retry a promise until it either resolves or fails a specified number of times. The `retry` function is passed to the promise-returning function, and it can be called to retry the operation.
const promiseRetry = require('promise-retry');
function myFunction() {
return new Promise((resolve, reject) => {
// An operation that may fail
});
}
promiseRetry(function (retry, number) {
console.log('attempt number', number);
return myFunction().catch(retry);
})
.then(function (value) {
// resolved value
})
.catch(function (error) {
// operation failed
});
Custom retry options
This feature allows you to specify custom options for retries, such as the maximum number of attempts, the factor by which the timeout increases, and the minimum and maximum timeout between retries.
const promiseRetry = require('promise-retry');
promiseRetry(function (retry, number) {
// Your promise-returning function
}, {
retries: 5, // Maximum number of attempts
factor: 2, // The exponential factor
minTimeout: 1 * 1000, // The number of milliseconds before starting the first retry
maxTimeout: 60 * 1000 // The maximum number of milliseconds between two retries
})
.then(function (value) {
// resolved value
})
.catch(function (error) {
// operation failed
});
The 'retry' package provides a general retry operation, which can be used to wrap both synchronous and asynchronous functions. Unlike 'promise-retry', it is not specifically tailored for promises but can be used with them. It offers a similar set of options to control the retry behavior.
Similar to 'promise-retry', 'async-retry' is designed to work with async/await syntax and promises. It provides a simple and flexible way to add retry functionality to asynchronous operations. It differs in its API design and may offer different customization options.
The 'p-retry' package is another alternative that focuses on retrying promises. It has a slightly different API and may offer different options or defaults. It is built to work seamlessly with async/await and provides a concise way to handle retries in promise-based workflows.
Retries a function that returns a promise, leveraging the power of the retry module to the promises world.
There's already some modules that are able to retry functions that return promises but they were rather difficult to use or do not offer an easy way to do conditional retries.
$ npm install promise-retry
Calls fn
until the returned promise ends up fulfilled or rejected with an error different than
a retry
error.
The options
argument is an object which maps to the retry module options:
retries
: The maximum amount of times to retry the operation. Default is 10
.factor
: The exponential factor to use. Default is 2
.minTimeout
: The number of milliseconds before starting the first retry. Default is 1000
.maxTimeout
: The maximum number of milliseconds between two retries. Default is Infinity
.randomize
: Randomizes the timeouts by multiplying with a factor between 1
to 2
. Default is false
.The fn
function will receive a retry
function as its first argument that should be called with an error whenever you want to retry fn
. The retry
function will always throw an error.
If there are retries left, it will throw a special retry
error that will be handled internally to call fn
again.
If there are no retries left, it will throw the actual error passed to it.
If you prefer, you can pass the options first using the alternative function signature promiseRetry([options], fn)
.
var promiseRetry = require('promise-retry');
// Simple example
promiseRetry(function (retry, number) {
console.log('attempt number', number);
return doSomething()
.catch(retry);
})
.then(function (value) {
// ..
}, function (err) {
// ..
});
// Conditional example
promiseRetry(function (retry, number) {
console.log('attempt number', number);
return doSomething()
.catch(function (err) {
if (err.code === 'ETIMEDOUT') {
retry(err);
}
throw err;
});
})
.then(function (value) {
// ..
}, function (err) {
// ..
});
$ npm test
Released under the MIT License.
FAQs
Retries a function that returns a promise, leveraging the power of the retry module.
The npm package promise-retry receives a total of 11,789,433 weekly downloads. As such, promise-retry popularity was classified as popular.
We found that promise-retry demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.