# ts-retry-promise

> retry for functions returning a promise

Latest version **0.8.1** (published 2024-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-retry-promise
pnpm add ts-retry-promise
yarn add ts-retry-promise
bun add ts-retry-promise
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.1 |
| Published | 2024-05-19 |
| First published | 2018-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 32.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 63 |
| Maintainers | martinmoeller |
| Keywords | promise, retry, typescript |

## Links

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

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.8.1 (latest) — 2024-05-19
- 0.0.9-0 (next) — 2018-03-06
- 0.8.0 — 2024-01-07
- 0.7.1 — 2023-08-29
- 0.7.0 — 2022-07-25
- 0.6.2 — 2022-07-01
- 0.6.1 — 2022-01-31
- 0.6.0 — 2020-10-24
- 0.5.0 — 2020-06-24
- 0.4.0 — 2020-05-27
- 0.3.1 — 2020-03-16
- 0.3.0 — 2020-03-15
- 0.2.0 — 2018-03-25
- 0.1.0 — 2018-03-22
- 0.0.10 — 2018-03-11
- … 13 more at https://npm.io/package/ts-retry-promise/versions

## README

# ts-retry-promise #

[![Build Status](https://github.com/normartin/ts-retry-promise/workflows/Build/badge.svg?branch=master)](https://github.com/normartin/ts-retry-promise/actions/workflows/ci.yml?query=branch%3Amaster)
[![Coverage Status](https://coveralls.io/repos/github/normartin/ts-retry-promise/badge.svg?branch=master)](https://coveralls.io/github/normartin/ts-retry-promise?branch=master)
[![Dependencies](https://img.shields.io/badge/Dependencies-none-brightgreen)](https://github.com/normartin/ts-retry-promise/blob/master/package.json)
[![NPM](https://img.shields.io/npm/v/ts-retry-promise.svg?color=#555)](https://www.npmjs.com/package/ts-retry-promise)

_retry for functions returning a promise_

[Changelog](https://github.com/normartin/ts-retry-promise/releases)

## Usage

Install with yarn:
`yarn add ts-retry-promise`

Install with npm:
`npm install --save ts-retry-promise`

Then you can import it with:

```typescript
import { retry } from 'ts-retry-promise';

const result: number = await retry(() => Promise.resolve(1), {retries: 3});
```

This will instantly start calling your function until it returns a resolved promise, no retries are left or a timeout occurred.

If you want to add retries to an existing function, use the decorator:

```typescript
import { retryDecorator } from 'ts-retry-promise';

const asyncFunction = async (s: String) => s;

const decoratedFunction = retryDecorator(asyncFunction, {timeout: 1});

const result: string = await decoratedFunction("1");
```

Here `decoratedFunction` is a function with the same signature as `asyncFunction`, but will do retries in case of failures.

## Configuration

Both `retry` and `retryDecorator` take an optional second argument where you can configure the number of retries and timeouts:

```typescript
export interface RetryConfig<T> {
    // number of maximal retry attempts (default: 10)
    retries?: number | "INFINITELY";

    // wait time between retries in ms (default: 100)
    delay?: number;

    // check the result, will retry until true (default: () => true)
    until?: (t: T) => boolean;

    // log events (default: () => undefined)
    logger?: (msg: string) => void;

    // overall timeout in ms (default: 60 * 1000)
    timeout?: number | "INFINITELY";

    // increase delay with every retry (default: "FIXED")
    backoff?: "FIXED" | "EXPONENTIAL" | "LINEAR" | ((attempt: number, delay: number) => number);

    // maximal backoff in ms (default: 5 * 60 * 1000)
    maxBackOff?: number;

    // allows to abort retrying for certain errors, will retry until false (default: () => true)
    retryIf: (error: any) => boolean
}
```

## Customize ##

_customizeRetry_ returns a new instance of _retry_ that has the defined default configuration.

```typescript
import { customizeRetry } from 'ts-retry-promise'; 

const impatientRetry = customizeRetry({timeout: 5});

await expect(impatientRetry(async () => wait(10))).to.be.rejectedWith("Timeout");

// another example

const retryUntilNotEmpty = customizeRetry({until: (array: any[]) => array.length > 0});

const result = await retryUntilNotEmpty(async () => [1, 2]);

expect(result).to.deep.eq([1, 2]);
```


You can do the same for decorators:
```typescript
import { customizeDecorator } from 'ts-retry-promise'; 

const asyncFunction = async (s: string) => {
    await wait(3);
    return s;
};

const impatientDecorator = customizeDecorator({timeout: 1});

expect(impatientDecorator(asyncFunction)("1")).to.be.rejectedWith("Timeout");
```

## Failure ##
In case `retry` failed, an _error_ is thrown. 
You can access the error that occurred the last time the function has been retried via the property `lastError`:
```typescript
retry(async () => throw "1")
    .catch(err => console.log(err.lastError)); // will print "1" 
```

## NotRetryableError ##
Wrapped function can throw `NotRetryableError` if retrying need to be stopped eventually:
```typescript
import { NotRetryableError } from 'ts-retry-promise';

retry(async () => { throw new NotRetryableError("This error") }, { retries: 'INFINITELY' })
    .catch(err => console.log(err.lastError.message));  // will print "This error"
```

## Samples ##

_retryDecorator_ can be used on any function that returns a promise

```typescript
const loadUserProfile: (id: number) => Promise<{ name: string }> = async id => ({name: "Mr " + id});

const robustProfileLoader = retryDecorator(loadUserProfile, {retries: 2});

const profile = await robustProfileLoader(123);
```


_retry_ is well suited for acceptance tests (but not restricted to)

```typescript
// ts-retry-promise/test/retry-promise.demo.test.ts
it("will retry until no exception or limit reached", async () => {

    await retry(async () => {
        const title = await browser.$("h1");
        expect(title).to.eq("Loaded");
    });

});

it("can return a result", async () => {

    const pageTitle = await retry(async () => {
        const title = await browser.$("h1");
        expect(title).to.be.not.empty;
        return title;
    });

    // do some stuff with the result
    expect(pageTitle).to.eq("Loaded");
});

it("can be configured and has defaults", async () => {

    await retry(async () => {
        // your code
    }, {backoff: "LINEAR", retries: 100});

});

it("will retry until condition is met or limit reached", async () => {

    await retry(
        () => browser.$$("ul"),
        {until: (list) => list.length === 2});

});

it("can have a timeout", async () => {

    const promise = retry(
        () => wait(100),
        {timeout: 10},
    );

    await expect(promise).to.be.rejectedWith("Timeout");
});

it("can create a customized retry", async () => {
    const impatientRetry = customizeRetry({timeout: 5});

    await expect(impatientRetry(async () => wait(10))).to.be.rejectedWith("Timeout");
});

it("can create another customized retry", async () => {
    const retryUntilNotEmpty = customizeRetry({until: (array: number[]) => array.length > 0});

    const result = await retryUntilNotEmpty(async () => [1, 2]);

    expect(result).to.deep.eq([1, 2]);
});

it("can customize default config", async () => {
    const originalTimeout = defaultRetryConfig.timeout;
    try {
        defaultRetryConfig.timeout = 1;

        await expect(retry(async () => wait(10))).to.be.rejectedWith("Timeout");
    } finally {
        defaultRetryConfig.timeout = originalTimeout;
    }
});
```

# Release instructions
Release automation has been setup according this [guide](https://michaelzanggl.com/articles/github-actions-cd-setup/).

1. Create a Github release with version tag like `0.6.1`. 
1. Check the new version exists on [npmjs.com/package/ts-retry-promise](https://www.npmjs.com/package/ts-retry-promise) and has `latest` tag.

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