Update OperationOptions to accept an array of timeouts in the retry module (#59861)

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

* Update async-retry typings accordingly

* Add new test for promise-retry
This commit is contained in:
Mazharul Onim
2022-04-26 15:16:48 -04:00
committed by GitHub
parent 667cea33b9
commit 6f77b38130
4 changed files with 34 additions and 4 deletions

View File

@@ -6,7 +6,7 @@
// 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>,
@@ -14,7 +14,7 @@ declare function AsyncRetry<A>(
): Promise<A>;
declare namespace AsyncRetry {
interface Options extends OperationOptions {
interface Options extends WrapOptions {
onRetry?: ((e: Error, attempt: number) => any) | undefined;
}

View File

@@ -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));
});
});

View File

@@ -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
@@ -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;

View File

@@ -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