source: trip-planner-front/node_modules/promise-retry/README.md@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
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
17Retries 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
19There's already some modules that are able to retry functions that return promises but
20they 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
32Calls `fn` until the returned promise ends up fulfilled or rejected with an error different than
33a `retry` error.
34The `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
43The `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.
44If there are retries left, it will throw a special `retry` error that will be handled internally to call `fn` again.
45If there are no retries left, it will throw the actual error passed to it.
46
47If you prefer, you can pass the options first using the alternative function signature `promiseRetry([options], fn)`.
48
49## Example
50```js
51var promiseRetry = require('promise-retry');
52
53// Simple example
54promiseRetry(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
67promiseRetry(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
94Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
Note: See TracBrowser for help on using the repository browser.