p-retry
Advanced tools
Comparing version 4.0.0 to 4.1.0
105
index.d.ts
import {OperationOptions} from 'retry'; | ||
export interface FailedAttemptError extends Error { | ||
readonly attemptNumber: number; | ||
readonly retriesLeft: number; | ||
} | ||
declare class AbortErrorClass extends Error { | ||
readonly name: 'AbortError'; | ||
readonly originalError: Error; | ||
export interface Options extends OperationOptions { | ||
/** | ||
* Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
*/ | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void; | ||
Abort retrying and reject the promise. | ||
@param message - Error message or custom error. | ||
*/ | ||
constructor(message: string | Error); | ||
} | ||
/** | ||
* Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason. | ||
* | ||
* It doesn't retry on `TypeError` as that's a user error. | ||
* | ||
* @param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value. | ||
* @param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
*/ | ||
export default function pRetry<T>( | ||
input: (attemptCount: number) => PromiseLike<T> | T, | ||
options?: Options | ||
): Promise<T>; | ||
declare namespace pRetry { | ||
interface FailedAttemptError extends Error { | ||
readonly attemptNumber: number; | ||
readonly retriesLeft: number; | ||
} | ||
export class AbortError extends Error { | ||
readonly name: 'AbortError'; | ||
readonly originalError: Error; | ||
interface Options extends OperationOptions { | ||
/** | ||
Callback invoked on each retry. Receives the error thrown by `input` as the first argument with properties `attemptNumber` and `retriesLeft` which indicate the current attempt number and the number of attempts left, respectively. | ||
*/ | ||
readonly onFailedAttempt?: (error: FailedAttemptError) => void; | ||
} | ||
type AbortError = AbortErrorClass; | ||
} | ||
declare const pRetry: { | ||
/** | ||
* Abort retrying and reject the promise. | ||
* | ||
* @param message - Error message or custom error. | ||
*/ | ||
constructor(message: string | Error); | ||
} | ||
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason. | ||
It doesn't retry on `TypeError` as that's a user error. | ||
@param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value. | ||
@param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module. | ||
@example | ||
``` | ||
import pRetry = require('p-retry'); | ||
import fetch from 'node-fetch'; | ||
const run = async () => { | ||
const response = await fetch('https://sindresorhus.com/unicorn'); | ||
// Abort retrying if the resource doesn't exist | ||
if (response.status === 404) { | ||
throw new pRetry.AbortError(response.statusText); | ||
} | ||
return response.blob(); | ||
}; | ||
(async () => { | ||
console.log(await pRetry(run, {retries: 5})); | ||
// With the `onFailedAttempt` option: | ||
const result = await pRetry(run, { | ||
onFailedAttempt: error => { | ||
console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`); | ||
// 1st request => Attempt 1 failed. There are 4 retries left. | ||
// 2nd request => Attempt 2 failed. There are 3 retries left. | ||
// … | ||
}, | ||
retries: 5 | ||
}); | ||
console.log(result); | ||
})(); | ||
``` | ||
*/ | ||
<T>( | ||
input: (attemptCount: number) => PromiseLike<T> | T, | ||
options?: pRetry.Options | ||
): Promise<T>; | ||
AbortError: typeof AbortErrorClass; | ||
// TODO: remove this in the next major version | ||
default: typeof pRetry; | ||
}; | ||
export = pRetry; |
@@ -67,4 +67,5 @@ 'use strict'; | ||
module.exports = pRetry; | ||
// TODO: remove this in the next major version | ||
module.exports.default = pRetry; | ||
module.exports.AbortError = AbortError; |
{ | ||
"name": "p-retry", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Retry a promise-returning or async function", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava && tsd-check" | ||
"test": "xo && ava && tsd" | ||
}, | ||
@@ -47,7 +47,7 @@ "files": [ | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"ava": "^1.4.1", | ||
"delay": "^4.1.0", | ||
"tsd-check": "^0.3.0", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.0" | ||
} | ||
} |
9071
124