Skip to content

Commit

Permalink
Update OperationOptions to accept an array of timeouts in the retry m…
Browse files Browse the repository at this point in the history
…odule (DefinitelyTyped#59861)

* Update OperationOptions to accept an array of timeouts in the retry module

* Update async-retry typings accordingly

* Add new test for promise-retry
  • Loading branch information
mionim authored Apr 26, 2022
1 parent 667cea3 commit 6f77b38
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions types/async-retry/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import { OperationOptions } from 'retry';
import { WrapOptions } from 'retry';

declare function AsyncRetry<A>(
fn: AsyncRetry.RetryFunction<A>,
opts?: AsyncRetry.Options
): Promise<A>;

declare namespace AsyncRetry {
interface Options extends OperationOptions {
interface Options extends WrapOptions {
onRetry?: ((e: Error, attempt: number) => any) | undefined;
}

Expand Down
25 changes: 25 additions & 0 deletions types/promise-retry/promise-retry-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,29 @@ describe("Promise-retry tests", () => {
.then((value: any) => console.log("Finished with value ", value))
.catch((err: any) => console.error(err.message || err));
});

it('should allow an array of timeouts as the param', () => {
let count = 0;

promiseRetry(
(retryCb, attemptNumber) => {
count += 1;

return Promise.resolve()
.then(() => {
console.log("Count in then()", count);
if (count > 1) return Promise.resolve('final');
else return Promise.reject(new Error('arbitrary excuse to retry'));
})
.catch((err: any) => {
console.log("Count in catch()", count);
if (count > 1) return Promise.resolve('final');
else return retryCb(err);
});
},
[2000, 3000, 4000]
)
.then((value: any) => console.log("Finished with value ", value))
.catch((err: any) => console.error(err.message || err));
});
});
6 changes: 4 additions & 2 deletions types/retry/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export interface AttemptTimeoutOptions {
*/
export function operation(options?: OperationOptions): RetryOperation;

export interface OperationOptions extends TimeoutsOptions {
export type OperationOptions = WrapOptions | number[];

export interface WrapOptions extends TimeoutsOptions {
/**
* Whether to retry forever.
* @default false
Expand Down Expand Up @@ -148,4 +150,4 @@ export interface CreateTimeoutOptions {
*
*/
export function wrap(object: object, methods?: string[]): void;
export function wrap(object: object, options?: OperationOptions, methods?: string[]): void;
export function wrap(object: object, options?: WrapOptions, methods?: string[]): void;
3 changes: 3 additions & 0 deletions types/retry/retry-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ operation.stop();
operation.reset();
operation.attempts(); // $ExpectType number

// accept an array of timeouts as well:
retry.operation([2000, 3000, 4000]);

retry.createTimeout(att); // $ExpectType number
retry.createTimeout(att, createTimeoutOptions); // $ExpectType number

Expand Down

0 comments on commit 6f77b38

Please sign in to comment.