UNPKG

1.32 kBTypeScriptView Raw
1import { WrapOptions } from "retry";
2
3/**
4 * Retrying made simple, easy, and async.
5 *
6 * @example
7 * import retry = require('async-retry');
8 * import fetch from 'node-fetch';
9 *
10 * await retry(
11 * async (bail) => {
12 * // if anything throws, we retry
13 * const res = await fetch('https://google.com');
14 *
15 * if (403 === res.status) {
16 * // don't retry upon 403
17 * bail(new Error('Unauthorized'));
18 * return;
19 * }
20 *
21 * const data = await res.text();
22 * return data.substr(0, 500);
23 * },
24 * {
25 * retries: 5,
26 * }
27 * );
28 */
29declare function AsyncRetry<TRet>(fn: AsyncRetry.RetryFunction<TRet>, opts?: AsyncRetry.Options): Promise<TRet>;
30
31declare namespace AsyncRetry {
32 interface Options extends WrapOptions {
33 /**
34 * An optional function that is invoked after a new retry is performed. It's passed the
35 * `Error` that triggered it as a parameter.
36 */
37 onRetry?: ((e: Error, attempt: number) => any) | undefined;
38 }
39
40 /**
41 * @param bail A function you can invoke to abort the retrying (bail).
42 * @param attempt The attempt number. The absolute first attempt (before any retries) is `1`.
43 */
44 type RetryFunction<TRet> = (bail: (e: Error) => void, attempt: number) => TRet | Promise<TRet>;
45}
46
47export = AsyncRetry;