# trier-promise

> A promise based trier

Latest version **1.0.1** (published 2016-10-14) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-10-14 |
| First published | 2016-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.1.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Guyon Roche |
| Maintainers | guyonroche |
| Keywords | trier, retry, promise, timeout |

## Links

- npm: https://www.npmjs.com/package/trier-promise
- npm.io page: https://npm.io/package/trier-promise

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-10-14
- 1.0.0 — 2016-10-14

## README

# trier-promise

A promise based trier

Do one thing and do it well many times.

# Installation

npm install trier-promise

# Interface

## Require

```javascript
var tp = require('trier-promise');

```

## Try something for 10 seconds at 1 second intervals

```javascript
tp({
    action: someFunctionThatReturnsAPromise,
    timeout: 10000,
    interval: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });
```

## Try something for 10 seconds but guarantee 1 second sleep time between calls

If the asynchronous function takes a while, you may not want overlapping calls. 

```javascript
tp({
    action: aFunctionWithStdAsyncSignature,
    timeout: 10000,
    sleep: 1000,
    async: true   // note: this is needed so that tp knows to call with callback and wait
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });
```

## Try something 10 times, no matter the time

```javascript
tp({
    action: aFunctionThatReturnsImmediately,
    limit: 10,
    sleep: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'LIMIT'
  });
```

## Try something until some condition

```javascript
tp({
    action: someFunctionThatReturnsAPromise,
    until: function() { return Math.random() < 0.1 },
    sleep: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'UNTIL'
  });
```


## Exponentially growing interval

Sometimes you want to start polling rapidly but slow down for subsequent calls.

So instead of using a Number value as the interval (or sleep), specify a function that returns a Number.

```javascript
var interval = 10;
tp({
    action: aFunctionWithStdAsyncSignature,
    timeout: 600000, // 10 minutes
    interval: function() { return interval *= 2; },
    async: true   // note: this is needed so that tp knows to call with callback and wait
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });
```

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