diff --git a/types/p-retry/index.d.ts b/types/p-retry/index.d.ts index 784f637549..c1791fcc63 100644 --- a/types/p-retry/index.d.ts +++ b/types/p-retry/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for p-retry 1.0 +// Type definitions for p-retry 2.0 // Project: https://github.com/sindresorhus/p-retry#readme // Definitions by: BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -8,7 +8,7 @@ import { OperationOptions } from 'retry'; export = pRetry; -declare function pRetry(input: (attemptCount: number) => PromiseLike | T, options?: OperationOptions): Promise; +declare function pRetry(input: (attemptCount: number) => PromiseLike | T, options?: pRetry.Options): Promise; declare namespace pRetry { class AbortError extends Error { @@ -16,4 +16,13 @@ declare namespace pRetry { readonly originalError: Error; constructor(message: string | Error); } + + interface FailedAttemptError extends Error { + attemptNumber: number; + attemptsLeft: number; + } + + interface Options extends OperationOptions { + onFailedAttempt?: (error: FailedAttemptError) => void; + } } diff --git a/types/p-retry/p-retry-tests.ts b/types/p-retry/p-retry-tests.ts index 72962d553e..1b58960e95 100644 --- a/types/p-retry/p-retry-tests.ts +++ b/types/p-retry/p-retry-tests.ts @@ -11,6 +11,11 @@ const run = () => fetch('https://sindresorhus.com/unicorn') return response.text(); }); -pRetry(run, {retries: 5}).then(result => { +pRetry(run, { + retries: 5, + onFailedAttempt: error => { + console.log(`Attempt ${error.attemptNumber} failed. There are ${error.attemptsLeft} attempts left.`); + } +}).then(result => { const str: string = result; });