// STEP 1: create function that returns promise
function sendRequest() {
return request('http://site.com/')
}
// STEP 2: Pass that function to the retry(FUNCTION_NAME)
retry(sendRequest)
.then(data => console.log('Connected π'))
.catch(error => console.log('Not connected π€·β'))
Checkout full example or step by step guide.
You can get it on npm.
npm install --save retryer
Take a look at our many examples:
basic
: How to retry promise.request
: How to retry HTTP request.mongoose
: How to reconnect to Mongoose.mongodb
: How to reconnect to MongoDB.redis
: How to reconnect to Redis.async/await
: How to retry async/await.bluebird
: How to retry bluebird promise.bunyan
: How to integrate bunyan logger.winston
: How to integrate winston logger.advanced-options
: How to use options and customise log messages.Couldn't find appropriate example?
Create an issue and we will prepare one πͺ
Change anything
you need to fit
your needs.
How to customise it?
You can easely integrate any custom logger. Take a look at examples:
Option Name | Type | Default | Description | Example |
---|---|---|---|---|
total |
Number |
10 |
Number of attempts to retry | options-total.js |
timeout |
Number |
1000 |
Backoff timeout (in ms) | options-timeout.js |
onStart |
Function |
function(attempt) {..} |
Is triggered on start for each attempt | options-on-start.js |
onError |
Function |
function(error, attempt) {..} |
Is triggered on error for each attempt | options-on-error.js |
Pass an options
as a second
argument.
// Syntax
retry(fn, OPTIONS);
// Example: retry request 3 times with 2.5s timeout
// const options = {
// timeout: 2500,
// total: 3,
// function(attempt) {..},
// function(err, attempt) {..}
// };
//
// retry(sendRequest, options);
Checkout full example or step by step guide.
Run npm run lint
to statically validate the code.
Run npm run test
to run unit tests.
Run npm run perf
to run the performance tests.
Please submit an issue on GitHub and provide information about your setup.
This project is licensed under the terms of the MIT license. See the LICENSE file.