# stubborn

> Retry engine

Latest version **1.2.5** (published 2019-05-26) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install stubborn
pnpm add stubborn
yarn add stubborn
bun add stubborn
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2019-05-26 |
| First published | 2014-05-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | Roman Grudzinski |
| Maintainers | grudzinski, kessler, yaniv |
| Keywords | retry |

## Links

- npm: https://www.npmjs.com/package/stubborn
- Repository: https://github.com/grudzinski/stubborn
- Issues: https://github.com/grudzinski/stubborn/issues
- npm.io page: https://npm.io/package/stubborn

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.11

## Recent versions

- 1.2.5 (latest) — 2019-05-26
- 1.2.4 — 2018-12-13
- 1.2.2 — 2018-12-13
- 1.2.1 — 2018-08-31
- 1.2.0 — 2014-06-30
- 1.1.0 — 2014-06-29
- 0.3.0 — 2014-06-26
- 0.2.0 — 2014-06-17
- 0.1.0 — 2014-06-17
- 0.0.1 — 2014-06-11
- 0.0.0 — 2014-05-11

## README

## Stubborn

A retry engine

### Install
```sh
npm install stubborn
```

### Example
```js
var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000
};

var stubborn = new Stubborn(task, options, callback);

stubborn.on('attemptError', onAttemptError);

stubborn.run();

function task(callback) {
  if (Math.random() > 0.2) {
    callback('Task error');
  } else {
    callback(null, 'Task result');
  }
}

function callback(err, result) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
}

function onAttemptError(err) {
  console.error(err);
}

```
### pluggable retry algorithm
All retry algorithms need the current number of attempts as input. As output, they are expected to produce a number that will be used as a factor of the delay.
```js
var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: Stubborn.exponentialBackoff()
};

var stubborn = new Stubborn(task, options, callback);

```
#### implement your own:
```js

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: function(attempts) {
    // delay next execution in options.delay * attempts * 2 
    // thus in attempt #2 we'll have 1000ms * 2 * 2 = 4 seconds delay
    return attempts * 2
  }
};
``` 
#### out of the box algorithms:
```
var Stubborn = require('stubborn');

var algo1 = Stubborn.exponentialBackoff(2)       // classic http://en.wikipedia.org/wiki/Exponential_backoff
var algo2 = Stubborn.simpleExponentialBackoff(2) // same as the above only without the random element
var algo3 = Stubborn.logarithmicProgression(2)   // logarithmic progression
var algo4 = Stubborn.linear(1, 0)                // ax+b
var algo5 = Stubborm.constant(1)                 // constant / fixed progression
```
#### configure using names instead of functions
```js
var options = {
  retryAlgorithm: 'linear',
  retryAlgorithmArgs: [ 1, 0 ]
}
```
### Methods
 * ```run``` starts specified task, call it only once
 * ```cancel``` stops retries

### Events
 * ```run```
 * ```onAttemptError```
 * ```schedule```

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