# @byungi/p-retry

> Retry when promise fails.

Latest version **0.2.2** (published 2020-01-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @byungi/p-retry
pnpm add @byungi/p-retry
yarn add @byungi/p-retry
bun add @byungi/p-retry
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2020-01-04 |
| First published | 2019-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8 |
| Dependencies | 2 |
| Unpacked size | 3.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | skt-t1-byungi |
| Maintainers | skt-t1-byungi |
| Keywords | promise, retry, retries, try |

## Links

- npm: https://www.npmjs.com/package/@byungi/p-retry
- Repository: https://github.com/skt-t1-byungi/promise/tree/master/packages/p-retry
- npm.io page: https://npm.io/package/@byungi/p-retry

## Dependencies (2)

- [@byungi/p-cancel](https://npm.io/package/@byungi/p-cancel.md) ^0.2.1
- [@byungi/promise-helpers](https://npm.io/package/@byungi/promise-helpers.md) ^0.2.0

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 0.2.2 (latest) — 2020-01-04
- 0.2.1 — 2019-12-30
- 0.2.0 — 2019-12-28
- 0.1.3 — 2019-09-21
- 0.1.2 — 2019-03-10
- 0.1.1 — 2019-03-09
- 0.1.0 — 2019-03-09
- 0.0.3 — 2019-01-21
- 0.0.2 — 2019-01-21
- 0.0.1 — 2019-01-15

## README

# @byungi/p-retry
Retry when promise fails.

## Example
```js
const runner = async () => {
    await asyncJobOrFail()
}

try{
    await pRetry(runner, {retries: 3})
}catch(err){
    console.log('3 retries, but all failed.')
}
```

## API
### pRetry(runner[, options])
Retry when promise fails.

#### runner
A function that retries when a promise is rejected. This function should return promise.

#### options
- `retries` - Number of retries. Default is 1.
- `interval` - Delay before retry. Default is 0.

### promise.cancel([reason])
Do not retry anymore and throw a `CancelError`.

```js
import pRetry, {CancelError} from '@byungi/p-retry'

const retryPromise = pRetry(asyncAlwaysFailRequest, {retries: 1000})

setTimeout(()=> retryPromise.cancel(), 100) // After 100ms, retry is aborted.

retryPromise.catch(err => {
    console.log(err.isCanceled) // => true
    console.log(err instanceof CancelError) // => true
    console.log(retryPromise.isCanceled) // => true
})
```

### promise.isCanceled
Returns whether the promise is canceled.

### promise.pipe(onFulfilled, onRejected)
Similar to `then` but can propagate `cancel` to the upper promise.

```js
const handleWithRequestPromise = retryPromise.pipe(response => {
    handleResponse(response)
})

handleWithRequestPromise.cancel() // => retry is aborted.
```

## License
MIT

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