
promise-retry-helper
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.
Installation
This package can be used in node and browser environments.
Node.js
Install package via npm
$ npm install promise-retry-helper
or via yarn
$ yarn add promise-retry-helper
Browser
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
.
Usage
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.
Example
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) {
return response;
}
return Promise.reject(response);
});
})
.then((response) => {
})
.catch((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) {
return response.json();
}
throw response
}
try {
const data = await retry(inputFunction, { retries: 2, delay: 500 });
} catch(error) {
}
License
Released under the MIT License.