Skip to content

ykrevnyi/retryer.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

51 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

retryer.js

retryer.js is a tiny, battle tested library that enables you to retry promises.

GitHub release Codacy Badge Build Status npm License

retryer.js intro

Quickstart

// 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 πŸ€·β€'))

Install

You can get it on npm.

npm install --save retryer

Examples & HOWTO

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 πŸ’ͺ

Fully customisable

Change anything you need to fit your needs.

retryer.js options

Custom loggers

You can easely integrate any custom logger. Take a look at examples:

Available Options

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

How to Pass Options?

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.

Development

Code Quality

Run npm run lint to statically validate the code.

Run npm run test to run unit tests.

Performance Tests

Run npm run perf to run the performance tests.

Need Help?

Please submit an issue on GitHub and provide information about your setup.

License

This project is licensed under the terms of the MIT license. See the LICENSE file.