1# node-promise-retry 2 3[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] 4 5[npm-url]:https://npmjs.org/package/promise-retry 6[downloads-image]:http://img.shields.io/npm/dm/promise-retry.svg 7[npm-image]:http://img.shields.io/npm/v/promise-retry.svg 8[travis-url]:https://travis-ci.org/IndigoUnited/node-promise-retry 9[travis-image]:http://img.shields.io/travis/IndigoUnited/node-promise-retry/master.svg 10[david-dm-url]:https://david-dm.org/IndigoUnited/node-promise-retry 11[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-promise-retry.svg 12[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-promise-retry#info=devDependencies 13[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-promise-retry.svg 14 15Retries a function that returns a promise, leveraging the power of the [retry](https://github.com/tim-kos/node-retry) module to the promises world. 16 17There's already some modules that are able to retry functions that return promises but 18they were rather difficult to use or do not offer an easy way to do conditional retries. 19 20 21## Installation 22 23`$ npm install promise-retry` 24 25 26## Usage 27 28### promiseRetry(fn, [options]) 29 30Calls `fn` until the returned promise ends up fulfilled or rejected with an error different than 31a `retry` error. 32The `options` argument is an object which maps to the [retry](https://github.com/tim-kos/node-retry) module options: 33 34- `retries`: The maximum amount of times to retry the operation. Default is `10`. 35- `factor`: The exponential factor to use. Default is `2`. 36- `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`. 37- `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`. 38- `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. 39 40 41The `fn` function will receive a `retry` function as its first argument that should be called with an error whenever you want to retry `fn`. The `retry` function will always throw an error. 42If there's retries left, it will throw a special `retry` error that will be handled internally to call `fn` again. 43If there's no retries left, it will throw the actual error passed to it. 44 45If you prefer, you can pass the options first using the alternative function signature `promiseRetry([options], fn)`. 46 47## Example 48```js 49var promiseRetry = require('promise-retry'); 50 51// Simple example 52promiseRetry(function (retry, number) { 53 console.log('attempt number', number); 54 55 return doSomething() 56 .catch(retry); 57}) 58.then(function (value) { 59 // .. 60}, function (err) { 61 // .. 62}); 63 64// Conditional example 65promiseRetry(function (retry, number) { 66 console.log('attempt number', number); 67 68 return doSomething() 69 .catch(function (err) { 70 if (err.code === 'ETIMEDOUT') { 71 retry(err); 72 } 73 74 throw err; 75 }); 76}) 77.then(function (value) { 78 // .. 79}, function (err) { 80 // .. 81}); 82``` 83 84 85## Tests 86 87`$ npm test` 88 89 90## License 91 92Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php). 93