Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
retry-as-promised
Advanced tools
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.
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));
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.
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.
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 promises when they fail
$ npm install --save retry-as-promised
$ yarn add retry-as-promised
var retry = require('retry-as-promised').default;
var warningFn = function(msg){ someLoggingFunction(msg, 'notice'); };
// Will call the until max retries or the promise is resolved.
return retry(function (options) {
// options.current, times callback has been called including this call
return promise;
}, {
max: 3, // maximum amount of tries
timeout: 10000 // throw if no response or error within millisecond timeout, default: undefined,
match: [ // Must match error signature (ala bluebird catch) to continue
Sequelize.ConnectionError,
'SQLITE_BUSY'
],
backoffBase: 1000 // Initial backoff duration in ms. Default: 100,
backoffExponent: 1.5 // Exponent to increase backoff each try. Default: 1.1
report: warningFn, // the function used for reporting; must have a (string, object) argument signature, where string is the message that will passed in by retry-as-promised, and the object will be this configuration object + the $current property
name: 'SourceX' // if user supplies string, it will be used when composing error/reporting messages; else if retry gets a callback, uses callback name in erroring/reporting; else (default) uses literal string 'unknown'
});
FAQs
Retry a failed promise
The npm package retry-as-promised receives a total of 1,841,588 weekly downloads. As such, retry-as-promised popularity was classified as popular.
We found that retry-as-promised demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
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.