Skip to content

Commit

Permalink
add a decorator function
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoeller committed May 24, 2020
1 parent adb8a05 commit cac558e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ import { retry } from 'ts-retry-promise';
const result = await retry(() => Promise.resolve(1), {retries: 3});
```

### Usage as a decorator

You can decorate an exisiting function like this:

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

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

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

const result : Promise<string> = decorated("1");
```

## Interface

```typescript
Expand Down
4 changes: 4 additions & 0 deletions src/retry-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export async function retry<T>(f: () => Promise<T>, config?: Partial<RetryConfig
return timeout(effectiveConfig.timeout, (done) => _retry(f, effectiveConfig, done));
}

export function retryDecorator<T, F extends (...args: any[]) => Promise<T>>(func: F, config?: Partial<RetryConfig<T>>): (...funcArgs: Parameters<F>) => Promise<T> {
return (...args: Parameters<F>) => retry(() => func(...args), config);
}

// tslint:disable-next-line
export function customizeRetry<T>(customConfig: Partial<RetryConfig<T>>): (f: () => Promise<T>, config?: RetryConfig<T>) => Promise<T> {
return (f, c) => {
Expand Down
33 changes: 33 additions & 0 deletions test/retry-promise.decorator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {expect} from "./index";
import {customizeRetry, defaultRetryConfig, retry, retryDecorator, wait} from "../src/retry-promise";

describe("Retry decorator test", () => {

it("can use decorator", async () => {
const asyncFunction: (s: string) => Promise<string> = async s => s;

const asyncFunctionDecorated = retryDecorator(asyncFunction);

expect(asyncFunctionDecorated("1")).to.eventually.eq("1")
});

it("can use decorator with custom config", async () => {
const asyncFunction: (s: string) => Promise<string> = async s => {
await wait(5);
return s;
};

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

expect(asyncFunctionDecorated("1")).to.be.rejectedWith("Timeout")
});

it("can use decorator with multiple args", async () => {
const asyncFunction: (s1: string, s2: string) => Promise<string> = async (s1, s2) => s1 + s2;

const asyncFunctionDecorated: (s1: string, s2: string) => Promise<string> = retryDecorator(asyncFunction);

expect(asyncFunctionDecorated("1", "2")).to.eventually.eq("12")
});

});
13 changes: 12 additions & 1 deletion test/retry-promise.demo.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "./index";
import {customizeRetry, defaultRetryConfig, retry, wait} from "../src/retry-promise";
import {customizeRetry, defaultRetryConfig, retry, retryDecorator, wait} from "../src/retry-promise";

describe("Retry Promise Demo", () => {

Expand Down Expand Up @@ -75,6 +75,17 @@ describe("Retry Promise Demo", () => {
}
});

it("can use decorator", async () => {

type AsyncFunc = (s: string) => Promise<string>

const asyncFunction: AsyncFunc = async s => s;

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

expect(asyncFunctionDecorated("1")).to.eventually.eq("1")
});

});

const browser = {
Expand Down

0 comments on commit cac558e

Please sign in to comment.