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