# like-retry

> Retry and backoff using generators

Latest version **1.1.1** (published 2022-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install like-retry
pnpm add like-retry
yarn add like-retry
bun add like-retry
```

## 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.1.1 |
| Published | 2022-07-28 |
| First published | 2022-07-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Lucas Barrena |
| Maintainers | lukks |

## Links

- npm: https://www.npmjs.com/package/like-retry
- Repository: https://github.com/LuKks/like-retry
- Issues: https://github.com/LuKks/like-retry/issues
- npm.io page: https://npm.io/package/like-retry

## Recent versions

- 1.1.1 (latest) — 2022-07-28
- 1.1.0 — 2022-07-28
- 1.0.2 — 2022-07-26
- 1.0.1 — 2022-07-25
- 1.0.0 — 2022-07-25
- 0.1.1 — 2022-07-25
- 0.1.0 — 2022-07-25

## README

# like-retry

Retry and backoff using generators.

![](https://img.shields.io/npm/v/like-retry.svg) ![](https://img.shields.io/npm/dt/like-retry.svg) ![](https://img.shields.io/badge/tested_with-tape-e683ff.svg) ![](https://img.shields.io/github/license/LuKks/like-retry.svg)

```
npm i like-retry
```

## Usage
```javascript
const retry = require('like-retry')

for await (const backoff of retry({ max: 3, delay: 3000 })) {
  try {
    const response = await axios.get(...)
    console.log(response.data)
  } catch (error) {
    console.log(backoff.left) // 3, 2, 1, 0
    await backoff(error) // 3s, 3s, 3s and finally throws
  }
}
```

## Jitter
```javascript
for await (const backoff of retry({ max: 5, delay: 1000, jitter: 500 })) {
  await backoff(new Error()) // 1489ms, 1142ms, 1276ms, 1088ms, 1337ms and finally throws
}
```

## Linear
```javascript
for await (const backoff of retry({ max: 5, delay: 3000, strategy: 'linear' })) {
  await backoff(new Error()) // 3s, 6s, 9s, 12s, 15s and finally throws
}
```

## Exponential
```javascript
for await (const backoff of retry({ max: 5, delay: 20, strategy: 'exponential' })) {
  await backoff(new Error()) // 0.4s, 1.6s, 3.6s, 6.4s, 10s and finally throws
}
```

## Array
```javascript
for await (const backoff of retry({ max: 5, strategy: [1000, 5000, 15000] })) {
  await backoff(new Error()) // 1s, 5s, 15s, 15s, 15s and finally throws
}
```

## Custom
```javascript
const strategy = ({ delay, count, jitter }) => delay ** count

for await (const backoff of retry({ max: 5, delay: 2, strategy })) {
  await backoff(new Error()) // 4ms, 16ms, 64ms, 256ms, 1024ms and finally throws
}
```

## None
It's the same as no setting any strategy.

```javascript
for await (const backoff of retry({ max: 3, delay: 3000, strategy: 'none' })) {
  await backoff(new Error()) // 3s, 3s, 3s and finally throws
}
```

## Default
Without options there will be no retries, delay or anything.

```javascript
for await (const backoff of retry()) {
  await backoff(new Error()) // will just throw
}
```

## License
MIT

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