# async-retry-handler

> Retry an asynchronous operation with customizable retry options

Latest version **1.0.6** (published 2023-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-retry-handler
pnpm add async-retry-handler
yarn add async-retry-handler
bun add async-retry-handler
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2023-10-08 |
| First published | 2023-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | jasbanza |
| Maintainers | hannibal_hacktor |
| Keywords | nodejs, automation |

## Links

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

## Dependencies (1)

- [retry](https://npm.io/package/retry.md) ^0.13.1

## Recent versions

- 1.0.6 (latest) — 2023-10-08
- 1.0.5 — 2023-09-28
- 1.0.4 — 2023-09-27
- 1.0.3 — 2023-09-26
- 1.0.2 — 2023-09-26
- 1.0.1 — 2023-09-25
- 1.0.0 — 2023-09-25

## README

# Async Retry Handler

Retry an asynchronous operation with customizable retry options.

## Installation

You can install this package via npm:

```bash
npm install async-retry-handler
```

## Usage

Import the `asyncRetryHandler` function and use it to retry asynchronous operations with custom retry options.

### Example:

```js
import asyncRetryHandler from "async-retry-handler";

// Define your asynchronous operation function (e.g., runCommand)
async function runCommand(commandArgs) {
  // Implement your asynchronous operation here
  // ...
}

// Define success criteria, onSuccess, and onFailure functions
function successCriteria(response) {
  // Implement your success criteria here
  if (response && response.stdout) {
    const parsedResponse = JSON.parse(response.stdout);
    // Check if the response is an object with pool information
    if (
      parsedResponse &&
      typeof parsedResponse === "object" &&
      parsedResponse.pool
    ) {
      return true;
    }
  }
  return false;
}

function onSuccess(response) {
  // Handle success here
  console.log("Operation succeeded:", response);
  return JSON.parse(response.stdout).pool;
}

function onFailure(error) {
  // Handle failure here
  console.error("Operation failed:", error);
}

// Use the asyncRetryHandler to retry the operation with custom options
const response = asyncRetryHandler({
  operationFunction: runCommand,
  operationFunctionArgs: [`osmosisd q poolmanager pool ${poolId}`],
  maxRetries: 10,
  minTimeout: 300,
  maxTimeout: 2000,
  maxRetryTime: 10 * 1000,
  successCriteria,
  onSuccess,
  onFailure,
});

// Handle the response as needed
```

## Options

The `asyncRetryHandler` function accepts the following options:

- `operationFunction`: The asynchronous operation function to retry.
- `operationFunctionArgs`: The arguments for the operation function.
- `maxRetries` (optional): The maximum number of retry attempts (default: 10).
- `minTimeout` (optional): The minimum time in milliseconds between retry attempts (default: 300).
- `maxTimeout` (optional): The maximum time in milliseconds between retry attempts (default: 2000).
- `maxRetryTime` (optional): The maximum time in milliseconds for all retry attempts combined (default: 10000).
- `successCriteria`: A function that determines if the operation response was successful. It must return a boolean based on your success criteria.
- `onSuccess` (optional): A callback function to be called if the operation succeeds.
- `onFailure` (optional): A callback function to be called if the operation fails after all retries.
- `factor` (optional): The factor by which the timeout between retries is multiplied (default: 1).
- `randomize` (optional): If true, randomizes the timeout between retries (default: false).
- `debug` (optional): If true, logs retry attempts for debugging (default: false).

## Contributing

If you find any issues or have suggestions for improvements, please feel free to open an issue or create a pull request in the [GitHub repository](https://github.com/jasbanza/async-retry-handler).


## License

This project is licensed under the MIT License. See the [LICENSE](https://github.com/jasbanza/async-retry-handler/blob/main/LICENSE) file for details.

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