
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
promise-retry-helper
Advanced tools
Small utility function to retry a Promise.
If this function does not meet your needs, there are other packages in npm registry that you can explore.
This package can be used in node and browser environments.
Install package via npm
$ npm install promise-retry-helper
or via yarn
$ yarn add promise-retry-helper
Simply load ./dist/promise-retry-helper.min.js
via script
tag.
<script src="./dist/promise-retry-helper.min.js"></script>
The function will be available globally under the name promiseRetryHelper
.
retry(input: Function, options?: object)
Calls input
function and waits until the returned Promise ends up fulfilled or rejected. If Promise is rejected, it will attempt to retry the input
function.
input
(required) - Function to execute. It should return a Promise.
options
(optional) - Configuration object:
retries
(optional, default: 3) - Maximum amount of times to retry the input function.delay
(optional, default: 1000) - Number of milliseconds between each retry.When you want to trigger a retry, you can just reject a Promise in input
function.
const retry = require('promise-retry-helper');
retry(() => {
const randomId = Math.floor(Math.random() * 2);
return fetch(`https://jsonplaceholder.typicode.com/todos/${randomId}`)
.then(response => {
if (response.ok) {
// Success, retry will not be triggered
return response;
}
// Trigger retry
return Promise.reject(response);
});
})
.then((response) => {
// Handle success
})
.catch((error) => {
// Handle error
});
Using with async/await
const retry = require('promise-retry-helper');
const inputFunction = async () => {
const randomId = Math.floor(Math.random() * 2);
const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${randomId}`);
if (response.ok) {
// Success, retry will not be triggered
return response.json();
}
// Trigger retry
throw response
}
try {
const data = await retry(inputFunction, { retries: 2, delay: 500 });
// Handle success
} catch(error) {
// Handle error
}
Released under the MIT License.
FAQs
Utility function to retry Promises
We found that promise-retry-helper 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.