https://github.com/abacaj/resolutejs
Finally get to retry during a Promise operation (works in modern browsers as well as nodejs), zero dependencies.
https://github.com/abacaj/resolutejs
api nodejs promise-operation retries retry-requests
Last synced: 7 months ago
JSON representation
Finally get to retry during a Promise operation (works in modern browsers as well as nodejs), zero dependencies.
- Host: GitHub
- URL: https://github.com/abacaj/resolutejs
- Owner: abacaj
- License: mit
- Created: 2016-02-10T05:00:49.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T18:42:40.000Z (over 8 years ago)
- Last Synced: 2025-02-26T04:31:53.443Z (7 months ago)
- Topics: api, nodejs, promise-operation, retries, retry-requests
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 23
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# resolute.js
Determined, and unwavering.[](https://github.com/abacaj/resolutejs/releases/download/v1.1.2/resolute.js)
[](https://github.com/abacaj/resolutejs)Finally get to retry during a Promise operation (works in modern browsers as well as node.js), zero dependencies.
It includes a very basic but reliable retry mechanism for a Promise operation.
Minimal requirements: 3kb unminified / 0 dependencies.
## Use Cases
- Database retry
- API retry
- Anything...## Packages
[NPM - resolutejs](https://www.npmjs.com/package/resolutejs)## Usage
Download resolute.js from [](https://github.com/abacaj/resolutejs/releases/download/v1.1.2/resolute.js), create a new instance as shown below
and optionally provide a callback that is executed every retry, which gives you the current retry count.
### Node.js
```javascript
var Resolute = require("./resolute");var somePromiseOperation = function() {
return new Promise(function(resolve, reject) {
// Make it fail...
if (err) return reject(err);
resolve("success");
});
};var resolute_options = {
// Reference to your Promise function, note: this Promise will always fail.
operation: somePromiseOperation,
// Maximum number of times to attempt
maxRetry: 5,
// Delay between retries in milliseconds
delay: 2000
};var resolute_callback = function(retryCount) {
console.log(retryCount);
};var resolute = new Resolute(resolute_options, resolute_callback);
// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});
```#### exponential backoff
By passing in the exponential backoff flag resolute will exponentially increase the waiting time between retries.
```javascript
...var resolute_options = {
// Reference to your Promise function, note: this Promise will always fail.
operation: somePromiseOperation,
// Maximum number of times to attempt
maxRetry: 5,
// Delay between retries in milliseconds
delay: 2000,
// exponentially increase wait time between retries
exponentialBackoff: true, //default false
};var resolute_callback = function(retryCount, delay) {
console.log(`Retry ${retryCount} in ${delay} ms`);
};var resolute = new Resolute(resolute_options, resolute_callback);
// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});
```### Browser
```html```
```javascript
var somePromiseOperation = function() {
return new Promise(function(resolve, reject) {
// Make it fail...
if (err) return reject(err);
resolve("success");
});
};var resolute_options = {
// Reference to your Promise function, note: this Promise will always fail.
operation: somePromiseOperation,
// Maximum number of times to attempt
maxRetry: 5,
// Delay between retries in milliseconds
delay: 2000
};var resolute_callback = function(retryCount) {
console.log(retryCount);
};var resolute = new Resolute(resolute_options, resolute_callback);
// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});```
Contributors
- [Andrej Kovcic](https://github.com/kovcic)
- [Morgan Bickle](https://github.com/m0rganic)