# @dyaa/async-retry

> Async retry with hooks

Latest version **1.0.1** (published 2019-02-05) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install @dyaa/async-retry
pnpm add @dyaa/async-retry
yarn add @dyaa/async-retry
bun add @dyaa/async-retry
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-02-05 |
| First published | 2019-01-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dyaa Eldin |
| Maintainers | dyaa |
| Keywords | async, retry, promise |

## Links

- npm: https://www.npmjs.com/package/@dyaa/async-retry
- Repository: https://github.com/dyaa/async-retry
- Homepage: https://github.com/dyaa/async-retry#readme
- Issues: https://github.com/dyaa/async-retry/issues
- npm.io page: https://npm.io/package/@dyaa/async-retry

## Alternatives

- [@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
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-02-05
- 1.0.0 — 2019-01-30

## README

### @dyaa/async-retry
[![npm version](https://badge.fury.io/js/%40dyaa%2Fasync-retry.svg)](https://badge.fury.io/js/%40dyaa%2Fasync-retry)
[![wercker status](https://app.wercker.com/status/0171c17c9f92eaaaf5885d42c44b73a5/s/master 'wercker status')](https://app.wercker.com/project/byKey/0171c17c9f92eaaaf5885d42c44b73a5)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dyaa/async-retry/issues)
[![Known Vulnerabilities](https://snyk.io/test/github/{username}/{repo}/badge.svg)](https://snyk.io/test/github/{dyaa}/{async-retry})

#### Problem

We needed a utility that helps us to retry an async function on it's Failure/Rejection and to be able to control the number of retries and the time interval between each retry also to block the retries in case of some logic provided.

#### Installation

```bash
yarn add @dyaa/async-retry
```

Or if you prefer npm!

```bash
npm i @dyaa/async-retry
```

#### Usage

```ts
import asyncRetry from '@dyaa/async-retry';
```

`asyncRetry` expects two parameters

- The first parameter is a function that returns a `Promise`.
- The second parameter is an options object (**all of them are optional**).

| Options    | Type     | Optional? | Default     | Description                                                                                                     |
| ---------- | -------- | --------- | ----------- | --------------------------------------------------------------------------------------------------------------- |
| retries    | number   | :heavy_check_mark:  | 5retries    | number of retries                                                                                               |
| interval   | number   | :heavy_check_mark:  | 5000ms      | base interval between retries                                                                                   |
| dontRetry  | function | :heavy_check_mark:  | () => false | A function that returns a boolean value. This boolean value is to check if we want the retry to continue or not |
| onComplete | function | :heavy_check_mark:  | null        | A hook function that's called on the completion of the retry also provides (err, count) in params                                                                                                              |
| onFailure  | function | :heavy_check_mark:  | null        | A hook function that's called on the failure of the retry also provides (err) in params                                                                                                  |
| onRetry    | function | :heavy_check_mark:  | null        | A hook function that's called on every retry also provides (err, count) in params                                                                                                               |

#### Example

```js
import asyncRetry from '@dyaa/async-retry';

const tryFetch = () => fetch('http://www.mocky.io/v2/5c59705d320000f31eba3880')
  .then(res => {
    if (res.status !== 200) {
      return Promise.reject(new Error(`Rejected because of statusCode is ${res.status}`));
    }

    return res.json();
  }).catch(e => Promise.reject(new Error(e)));

asyncRetry(tryFetch, { 
  retries: 3,
  onRetry: (err, count) => console.log(`#### Retry #${count} with ${err}.`),
  onComplete: count => console.log(`#### Completed after ${count} retries.`),
  onFailure: err => console.log(`#### Failed because ${err}.`),
})
  .then(response => {
    console.log({response})
  })
  .catch(console.error);
```

<!--##### Backoff formula

```ts
/**
 * Backoff formula for more info checkout
 * https://en.wikipedia.org/wiki/Exponential_backoff
 * E -> factor
 * c -> number of trys
 * I -> Interval
 *
 * E(c) = (2^c - 1) / 2
 * E(c) * I
 * @param {number} times - The number of which retry.
 * @param {number} interval - Time interval of last retry in ms.
 * @returns {number} - Number of next retry interval in ms.
 */
```-->

#### Inspiration

- README structure [Kent C. Dodds's Tweet][0]

#### Thanks

- [Christian Gill](https://github.com/gillchristian) for reviewing and providing support.

#### License

[MIT License][1] © [dyaa][2]

[0]: https://twitter.com/kentcdodds/status/877189118572191744
[1]: https://github.com/dyaa/async-retry/blob/master/LICENSE
[2]: https://dyaa.me

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