# @amphibian/promise-retry

> promise retries

Latest version **1.2.0** (published 2021-12-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @amphibian/promise-retry
pnpm add @amphibian/promise-retry
yarn add @amphibian/promise-retry
bun add @amphibian/promise-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.2.0 |
| Published | 2021-12-06 |
| First published | 2019-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Thomas Lindstrøm |
| Maintainers | thomaslindstr_m |

## Links

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

## Dependencies (1)

- [@babel/runtime-corejs2](https://npm.io/package/@babel/runtime-corejs2.md) ^7.16.3

## Recent versions

- 1.2.0 (latest) — 2021-12-06
- 1.1.4 — 2019-09-06
- 1.1.3 — 2019-09-06
- 1.1.2 — 2019-08-08
- 1.1.1 — 2019-07-12
- 1.1.0 — 2019-06-18
- 1.0.2 — 2019-06-18
- 1.0.1 — 2019-03-26
- 1.0.0 — 2019-01-25

## README

# @amphibian/promise-retry

promise retries

```
npm install @amphibian/promise-retry
```

## Usage

```javascript
import retry from '@amphibian/promise-retry';

async function getUsers() {
	const response = await retry(() => fetch('https://reqres.in/api/users'));
	const user = await response.json();

	return user;
}
```

### With options

```javascript
retry(() => (
	fetch('https://reqres.in/api/users').then((response) => response.json())
), {attempts: 3, timeout: 250});
```

### Intentionally aborting retries

```javascript
const fetchUsers = () => (
	fetch('https://reqres.in/api/users')
		.then((response) => {
			if (response.status === 404) {
				// This will prevent subsequent retries
				throw new retry.AbortError('resource_not_found');

				// You can also clone an existing error
				const error = new Error('my custom error');
				error.code = 'my custom property';

				// Properties from `error` are cloned
				throw new retry.AbortError(error);
			}

			return response.json();
		})
);

retry(fetchUsers, {attempts: 3})
	.catch((error) => {
		console.log(error.name); // > AbortError
	});
```

## `retry(function, options)`

### `function` _(`Function`)_

The function to retry. Should return a `Promise` or be `async`.

### `options` _(`Object`)_

#### `options.attempts` _(`Number`)_

**Default**: `3`
Number of times to retry before throwing the error from the final attempt.

#### `options.timeout` _(`Number`)_

**Default**: `250`
Number of milliseconds to wait between retries.

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