# retrying-promise

> Create fault tolerant promises that retry upon failure according to a retry strategy.

Latest version **0.0.4** (published 2016-05-27) · 0 weekly downloads

## Install

```sh
npm install retrying-promise
pnpm add retrying-promise
yarn add retrying-promise
bun add retrying-promise
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2016-05-27 |
| First published | 2016-02-25 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=5.6.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Wouter Van den Broeck |
| Maintainers | wouter_vdb |
| Keywords | promise, retry strategy, fault tolerant, ECMAScript 2015, es6, js, javascript, node, node.js |

## Links

- npm: https://www.npmjs.com/package/retrying-promise
- Repository: https://github.com/wouter-vdb/retrying-promise
- Homepage: https://github.com/wouter-vdb/retrying-promise#readme
- npm.io page: https://npm.io/package/retrying-promise

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.4 (latest) — 2016-05-27
- 0.0.2 — 2016-02-25
- 0.0.1 — 2016-02-25

## README

# retrying-promise

> Create fault tolerant promises that retry upon failure according to a retry strategy.

The implementation of the retry strategies reuses code from the [node-retry][] project.


## Usage

Install `retrying-promise`:

```shell
npm install retrying-promise
```

Import `retrying-promise`:

    const retryPromise = require('retrying-promise');

The constant `retryPromise` is a function that returns a promise. Use this function similar to how you call the `new Promise()` constructor, but pass it a function that takes three instead of two functions as arguments: `resolve`, `retry` and `reject`.

    var promise = retryPromise(function (resolve, retry, reject) {

        resolve(result);  // the promise resolves normally

        retry(error);     // the promise failed and a retry may be attempted

        reject(error);    // the promise failed and no retry should be attempted

    });

Call the `resolve` function when the promise resolves normally.

Call the `retry` function when the promise failed and a retry may be attempted.

Call the `reject` function when the promise failed and no retry should be attempted.



## Test

Install [Mocha][].

```shell
sudo npm install -g mocha
```

Run tests:

```shell
npm test
```


## API Reference
**Author:** Wouter Van den Broeck  
**Copyright**: 2016  
<a name="exp_module_retrying-promise--module.exports"></a>
### module.exports([options], executor) ⇒ <code>Promise</code> ⏏
Returns a promise that conditionally tries to resolve multiple times, as specified by the retry
policy.

**Kind**: Exported function  

| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>retryPolicy</code> | Either An object that specifies the retry policy. |
| executor | <code>retryExecutor</code> | A function that is called for each attempt to resolve the promise. |

<a name="module_retrying-promise--module.exports..createTimeout"></a>
#### module.exports~createTimeout(attempt, opts) ⇒ <code>number</code>
Get a timeout value in milliseconds.

**Kind**: inner method of <code>[module.exports](#exp_module_retrying-promise--module.exports)</code>  
**Returns**: <code>number</code> - The timeout value in milliseconds.  

| Param | Type | Description |
| --- | --- | --- |
| attempt | <code>number</code> | The attempt count. |
| opts | <code>Object</code> | The options. |

<a name="module_retrying-promise--module.exports..retryPolicy"></a>
#### module.exports~retryPolicy : <code>Object</code>
An object that specifies the retry policy.

**Kind**: inner typedef of <code>[module.exports](#exp_module_retrying-promise--module.exports)</code>  
**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| retries | <code>number</code> | <code>10</code> | The maximum amount of times to retry the operation. |
| factor | <code>number</code> | <code>2</code> | The exponential factor to use. |
| minTimeout | <code>number</code> | <code>1000</code> | The number of milliseconds before starting the first retry. |
| maxTimeout | <code>number</code> | <code>Infinity</code> | The maximum number of milliseconds between two retries. |
| randomize | <code>boolean</code> | <code>false</code> | Randomizes the timeouts by multiplying with a factor between 1 to 2. |

<a name="module_retrying-promise--module.exports..retryExecutor"></a>
#### module.exports~retryExecutor : <code>function</code>
The function that is called for each attempt to resolve the promise.

**Kind**: inner typedef of <code>[module.exports](#exp_module_retrying-promise--module.exports)</code>  

| Param | Type | Description |
| --- | --- | --- |
| resolveFn | <code>function</code> | To be called when the promise resolves normally. |
| retryFn | <code>function</code> | To be called when the promise failed and a retry may be attempted. |
| [rejectFn] | <code>function</code> | To be called when the promise failed and no retry should be attempted. |


* * *

&copy; 2016, Wouter Van den Broeck


[Mocha]: http://mochajs.org
[node-retry]: https://github.com/tim-kos/node-retry

---
_Source: https://npm.io/package/retrying-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
